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

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

问题描述

我想打一个自定义的对话框。因为我不喜欢它的风格,我想有圆角的矩形,而不是尖角。我知道如何通过主题的Andr​​oidManifest.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>

我如何能够通过扩展实现了类似的结果,对话框 AlertDialog

推荐答案

在你的类的构造函数,扩展对话框调用超(背景下,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.

但是,如果主题是关于对话框,你想改变的唯一的事情,你可以尝试只实例化对话框类的实例,并把它传递的主题ID喜欢对话对话框=新的对话框(背景下,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);

延长对话的一个例子:

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);  
    }
}

的code,你会加重该类的其余部分将是pretty的多少完全一样,你会写在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.

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

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