2012-02-06 60 views
2

我目前正在創建一個自定義對話框(alertdialog不夠靈活,我的需要)。我希望它看起來像一個AlertDialog,具有良好的AlertDialog頭(見下圖)。自定義對話框,看起來像一個AlertDialog

另外我在我的自定義對話框中有一個listview,我希望它看起來像AlertDialog的列表視圖。我怎麼能這樣做?

到目前爲止,我在attr中找到了alertDialogTheme和alertDialogStyle。類,但我不知道如何使用它。

它應該是什麼樣子使用自定義對話框: typical alertdialog with listview

是什麼樣子(黑色背景,錯報頭):custom

我的佈局是非常基本的,看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_root" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

listview包含使用此佈局的項目

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:src="@drawable/icon_noimage" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:textColor="#ffffff" 
     android:textSize="18dp" /> 

</LinearLayout> 

有什麼想法?

--->解決方法使用alerdialog與自定義視圖,但現在我得到了下面的問題與一些差距:利用典型AlertDialog標題/頭

gapsproblem

回答

5

您可以創建一個正常的AlertDialog但隨後設置對話框的內容使用自定義視圖:

// Your custom layout can be whatever you want 
View customLayout = getLayoutInflater().inflate(R.layout.customLayout, null); 

// Create the dialog box 
AlertDialog myDialog = new AlertDialog.Builder(this) 
    .setTitle("My Title") 
    .setView(customLayout) 
    .create(); 

// Show the dialog box 
myDialog.show(); 

這將導致一個正常的期待AlertDialog與正常外觀標題,但內容將使用無論您想要的自定義視圖。

對於您希望顯示爲內容的ListView,您只需要重新創建視圖,只要您認爲合適,並在我的示例中將其用作customLayout即可。

+0

嗯好頭應該工作。但我提供的列表是動態創建的。我不確定你的意思是重新創建列表視圖項目。你能更精確地解釋一下如何讓自定義listview的alertdialog listview看起來像customLayout一樣嗎? – masi 2012-02-06 23:49:32

+1

據我所知,沒有可以繼承的默認主題或類似的東西。您只需手動佈置項目並設置邊距和填充以及字體大小以使其與您想要的匹配。僅供參考,您可以在創建'AlertDialog'之前動態更改'AlertDialog'列表中的項目。爲什麼這不適合你? – 2012-02-07 16:36:07

+0

好吧,它現在就像你現在所說的那樣工作,除了我還有一個小小的錯誤,我用一個問題的示例圖像編輯了初始問題:我無法擺脫那些空白! – masi 2012-02-07 18:58:16