警报对话框背景主题/颜色

问题描述 投票:6回答:2

我想设置AlertDialogue主题或更改背景颜色。

虽然我知道它有一个默认主题,但在不同的版本我得到不同的主题所以我想为所有版本修复它。

或者只是将背景颜色更改为白色

 @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.lib_dialog_image,
                new String[]{"name", "imageID","Spacing"}, new int[]{R.id.text1, R.id.image1,R.id.spacing});
        return new AlertDialog.Builder(getContext()).setAdapter(adapter,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ((PlaceCallActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID"));
                    }
                }).setCancelable(true).setTitle("PICK YOUR AVATAR").setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
    }

不要发布您的代码,请告诉我应该在哪里进行更改。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));

注意:上面的行会这样做,但我想知道我应该为我的AlertDialogue提供样式

enter image description here

android android-alertdialog
2个回答
12
投票

styles.xml文件中创建您的样式,如下所示。

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/White</item>
        <item name="android:textStyle">bold</item>
        <item name="android:headerDividersEnabled">true</item>
        <item name="android:typeface">normal</item>
        <item name="android:background">@color/colorPrimaryDark</item>
    </style>

然后使用Builder创建Alert Dialog,如下所示

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this/getActvity(), R.style.AlertDialogCustom));

这里将当前类的Context和样式传递给ContextThemeWrapper类构造函数。


3
投票

您应该在res / values / styles.xml中添加对话框样式。如下所示。

<style name="MyDialog" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:background">@color/white</item>
    </style>

或者您也改变背景颜色如下:

编辑:

 getWindow().setBackgroundDrawableResource(R.color.white);
© www.soinside.com 2019 - 2024. All rights reserved.