2011-04-20 74 views
7

我在底部有一個保存並取消按鈕的活動。活動的AlertDialog樣式按鈕

在AlertDialog中,按鈕顯示在某種樣式的容器視圖中。

我怎麼能給我的活動中的按鈕相同的外觀?具體來說,我如何應用AlertDialog中按鈕容器視圖的樣式來在包含按鈕的Activity中說出LinearLayout?

感謝

回答

1

我做一些事情是這樣的:

LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null); 

我然後使用dialogLayout調用findViewById()中的按鈕和其他視圖和設置OnClickListeners和這樣...

然後顯示對話框:

builder = new AlertDialog.Builder(this); 

builder.setView(dialogLayout); 

builder.create().show(); 
+0

謝謝,我會仔細研究,看看我能否實現它。說實話,我希望我可以只添加一個屬性給我在Activity的xml佈局文件中定義的LinearLayout,就像對背景圖像的簡單引用或其他東西! – Jodes 2011-04-20 19:29:24

+0

我從來沒有想過如果有任何方法來修改「正面」,「負面」和「中性」按鈕的背景圖像......這是我可以解決它的唯一方法。 – Maximus 2011-04-20 19:45:08

+0

感謝您再次回覆,我不明白您的代碼是如何工作的,我會在哪裏放置它?在具有我想要樣式的按鈕的活動的onCreate中?很困惑! – Jodes 2011-04-20 20:50:53

30

有解決方案摹iven elsewhere工作。總之,您可以簡單地在xml中使用style屬性來實現此目的。例如,style="?android:attr/buttonBarStyle"style="?android:attr/buttonBarButtonStyle"將完成這項工作(對於API 11+)。這是兩個水平放在一起的按鈕的例子。

<LinearLayout 
    style="?android:attr/buttonBarStyle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:measureWithLargestChild="true" 
    android:orientation="horizontal" 
    android:paddingTop="0dip" > 

    <Button 
     style="?android:attr/buttonBarButtonStyle" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text= "Ok" /> 

    <Button 
     style="?android:attr/buttonBarButtonStyle" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Cancel" /> 
</LinearLayout> 

剩下的唯一事情是,有一個水平線的正上方在alertDialog的按鈕,其中,上述代碼將不會創建。如果你想獲得水平線,應該在xml中手動添加,在LinearLayout之上。這將給你的水平線:

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:layout_marginBottom="0dp" 
    android:background="?android:attr/dividerVertical" /> 
+1

哇,這是偉大的你保存我的項目:) – Michal 2013-12-21 22:08:15

+1

非常輝煌。賞金途中! – Fattie 2014-05-23 12:08:58

+0

@JoeBlow謝謝! – hadi 2014-05-27 21:34:01