簡體   English   中英

警報對話框背景主題/顏色

[英]Alert Dialog background theme/Color

我想設置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提供樣式

在此輸入圖像描述

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類構造函數。

您應該在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM