2009-12-29 59 views
19

我想定製Dialog。因爲我不喜歡它的風格,所以我想要有圓角矩形而不是尖角。我知道如何通過主題AndroidManifest.xml實現它,比如,我用:通過擴展Dialog或AlertDialog來定製對話框

android:theme="@style/Theme.CustomDialog" 

而且Theme.CustomDialog.xml

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> 
     <item name="android:windowBackground">@drawable/filled_box</item> 
     <item name="android:windowNoTitle">true</item> 

filled_box.xml 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff"/> 
    <stroke android:width="3dp" color="#ffff8080"/> 
    <corners android:radius="30dp" /> 
    <padding android:left="10dp" android:top="10dp" 
     android:right="10dp" android:bottom="10dp" /> 
</shape> 

我怎樣才能實現通過擴展DialogAlertDialog類似的結果呢?

回答

41

在擴展Dialog的類的構造函數中調用super(context, R.style.CustomDialog);我已經做了很多次這樣的事情來創建具有特定主題的自定義對話框。

但是,如果主題是關於對話框,你想改變的唯一的事情,你可以嘗試只實例化對話框類的實例,並通過它像Dialog dialog = new Dialog(context, R.style.CustomDialog);

擴展對話框的示例主題ID:

public class MyDialog extends Dialog 
{ 
    public MyDialog(final Context context) 
    { 
     // Set your theme here 
     super(context, R.style.MyDialogTheme); 

     // This is the layout XML file that describes your Dialog layout 
     this.setContentView(R.layout.myDialogLayout); 
    } 
} 

您將添加到此類中的其餘代碼將與您在Activity類中編寫的內容非常相似。

+0

是的,謝謝你的幫助。我用Dialog dialog = new Dialog(context,R.style.CustomDialog),工作得很好。 但我不能寫延伸對話框,你能給我一些代碼片段 – pengwang 2009-12-30 00:50:53

+0

我加了一個擴展對話框的例子。 – 2009-12-30 01:22:07

+0

是否可以像我們在對話框中那樣設置正面和負面的按鈕? I:E .setPositiveButton( 「OK」, 新DialogInterface.OnClickListener(){ 公共無效的onClick(DialogInterface對話,詮釋whichButton){// 正面按鈕點擊 getActivityInstance()onOkClicked(GeneralDialogFragment.this)。 } } – 2017-04-28 11:10:39