通过扩展 Dialog 或 AlertDialog 自定义对话框 [英] Customizing dialog by extending Dialog or AlertDialog

查看:35
本文介绍了通过扩展 Dialog 或 AlertDialog 自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a custom Dialog. Because I don't like its style, I want to have rounded rectangle rather than sharp corners. I know how to implement it by theme in AndroidManifest.xml, for example, I use:

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

How can I implement a similar result by extending the Dialog or AlertDialog?

推荐答案

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

In the constructor of your class that extends Dialog call super(context, R.style.CustomDialog); I've done this many times to create custom dialogs with specific themes.

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

However if the theme is the only thing about the Dialog that you want to change, you could try just instantiating an instance of the Dialog class and pass it the theme ID like Dialog dialog = new Dialog(context, R.style.CustomDialog);

一个扩展Dialog的例子:

An example of extending 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 类中编写的代码非常相似.

The rest of the code you will add to this class is going to be pretty much exactly like what you would write in an Activity class.

这篇关于通过扩展 Dialog 或 AlertDialog 自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆