【问题标题】:Customizing dialog by extending Dialog or AlertDialog通过扩展 Dialog 或 AlertDialog 自定义对话框
【发布时间】:2010-12-30 19:12:12
【问题描述】:

我想定制一个Dialog。因为我不喜欢它的风格,我想要圆角矩形而不是尖角。我知道AndroidManifest.xml中的主题如何实现,例如我用:

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 来实现类似的结果?

【问题讨论】:

    标签: android customization android-dialog android-theme


    【解决方案1】:

    在扩展 Dialog 调用 super(context, R.style.CustomDialog); 的类的构造函数中,我已经多次这样做来创建具有特定主题的自定义对话框。

    但是,如果主题是您想要更改的 Dialog 的唯一内容,您可以尝试仅实例化 Dialog 类的实例并将主题 ID 传递给它,例如 Dialog dialog = new Dialog(context, R.style.CustomDialog);

    一个扩展Dialog的例子:

    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 类中编写的代码非常相似。

    【讨论】:

    • 是的,谢谢您的帮助。我使用了 Dialog dialog = new Dialog(context, R.style.CustomDialog),效果很好。但是我不能写extends Dialog,你能给我一些代码sn-ps
    • 我添加了一个扩展Dialog的例子。
    • 是否可以像我们在Dialog中那样设置正负按钮? i:e .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 正按钮点击 getActivityInstance().onOkClicked(GeneralDialogFragment.this); } }
    • 如果需要对话框不是全屏,只需使用:public MyDialog(final Context context) { super(context); this.setContentView(R.layout.myDialogLayout); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多