2010-08-26 89 views
2

好了,所以我讀過的自定義對話框的解釋和DEV網站上 http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialogAndroid:如何使用滾動標題創建對話框?

它展示的你如何做一個自定義對話框,但不知道如何自定義標題!

基本上我的標題太長了,我希望它滾動(如textview)或更好的仍然有一個'選取框'的效果,我認爲它被稱爲。

或者如果我不能讓它滾動,給它更多的空間來包裝更多的線!

任何想法,我不抱太大的希望,因爲它不是在android.dev :-(

回答

6

Customizig窗口(也因此而對話)的標題可以通過請求窗口功能CUSTOM_TITLE來完成,必須。在對話框/活動的setContentView之前完成

,這樣子類的onCreate(),請撥打以下:

super.onCreate(savedInstance); 
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // <- insert this 

然後,你的setContentView,這樣做:

setContentView(R.layout.main); 
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // <- insert this 

佈局通常可以包含任何你想要的。 對於選取框文本控件。例如做到這一點:

佈局/ custom_title.xml:

<FrameLayout android:id="@+id/FrameLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <TextView android:id="@+id/caption_text" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:text="This is a very very long text that will not fit into a caption regularly, so it will be displayed using marquee..." 
     android:lines="1" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:scrollHorizontally="true" 
     android:marqueeRepeatLimit="marquee_forever" 
     android:ellipsize="marquee" 
     ></TextView> 
</FrameLayout> 

由於與字幕功能的一些約束,文本視圖要作出可成爲焦點,它只會滾動聚焦時(它最初應該是)。

+0

謝謝 - 我會稍後再試 – Blundell 2010-09-09 14:44:47

+0

如果我必須使用setTitle(myString);在我的活動? – Kishore 2014-01-02 07:57:33

7

你可以讓對話框的標題多:

TextView title = (TextView) dialog.findViewById(android.R.id.title); 
title.setSingleLine(false); 
+0

我認爲這個答案比在沒有必要的時候繼承對話要好一些。 – 2014-11-05 11:28:03

0

我認爲(用於獲取的TextView)RuslanK的組合Thorstenvv的(製作的TextView滾動)的回答是最好的做法。