我想打一个自定义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实现类似的结果?

有帮助吗?

解决方案

在你的类扩展对话框呼叫super(context, R.style.CustomDialog);构造我做过很多次了与特定主题创建自定义对话框。

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

代码将添加到此类的其余部分将是非常酷似你会在一个Activity类写的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top