通过扩展Dialog或AlertDialog来自定义对话框

pen*_*ang 19 customization android android-theme android-dialog

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

android:theme="@style/Theme.CustomDialog"
Run Code Online (Sandbox Code Playgroud)

而且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>
Run Code Online (Sandbox Code Playgroud)

如何通过扩展DialogAlertDialog?来实现类似的结果?

Mar*_*k B 44

在扩展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);  
    }
}
Run Code Online (Sandbox Code Playgroud)

您将添加到此类的其余代码将与您在Activity类中编写的代码完全相同.