首页 文章

如何使用appCompat 22.1及更高版本的新AlertDialog并设置其样式

提问于
浏览
141

我正在尝试从默认的android AlertDialog 迁移到appCompat-22.1中包含的新内容 . 到目前为止,我了解您只需要导入 android.support.v7.app.AlertDialog 包以便使用它 .

但我该如何设计呢?例如,更改正/负按钮颜色, Headers 颜色,消息颜色和背景颜色?

5 回答

  • 56

    创建 AlertDialog 时,您可以设置要使用的主题 .

    Example - Creating the Dialog

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    builder.setTitle("AppCompatDialog");
    builder.setMessage("Lorem ipsum dolor...");
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();
    

    styles.xml - Custom style

    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">#FFC107</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#FFFFFF</item>
        <!-- Used for the background -->
        <item name="android:background">#4CAF50</item>
    </style>
    

    Result

    styled alertdialog

    Edit

    要更改 Headers 的外观,您可以执行以下操作 . 首先添加一种新风格:

    <style name="MyTitleTextStyle">
        <item name="android:textColor">#FFEB3B</item>
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
    </style>
    

    之后只需在_657744中引用此样式:

    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        ...
        <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
    </style>
    

    这样,您可以通过 android:textColorPrimary 为消息定义不同的 textColor ,并通过样式为 Headers 定义不同的 textColor .

  • -2

    要为所有应用程序使用主题,请不要使用第二个参数来设置Dialog的样式

    <style name="MyTheme" parent="Base.Theme.AppCompat.Light">
        <item name="alertDialogTheme">@style/dialog</item>
        <item name="colorAccent">@color/accent</item>
    </style>
    
    <style name="dialog" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/accent</item>
    </style>
    

    在我的应用程序中使用主题中的颜色重音不显示带主题colorAccent的alertDialog按钮我必须在主题中添加对话框样式 .

  • 4

    如果你想使用新的android.support.v7.app.AlertDialog并为按钮设置不同的颜色,并且还有自定义布局,那么看看我的https://gist.github.com/JoachimR/6bfbc175d5c8116d411e

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        View v = inflater.inflate(R.layout.custom_layout, null);
    
        initDialogUi(v);
    
        final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
                .setTitle(getString(R.string.some_dialog_title))
                .setCancelable(true)
                .setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doSomething();
                                dismiss();
                            }
                        })
                .setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dismiss();
                            }
                        })
                .setView(v)
                .create();
    
        // change color of positive button         
        d.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setTextColor(getResources().getColor(R.color.colorPrimary));
            }
        });
    
        return d;
    }
    

    enter image description here

  • 18

    关注@reVerse的答案,但在我的情况下,我已经在我的 AppTheme 中有了一些属性

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:textColor">#111</item>
        <item name="android:textSize">13sp</item>
    </style>
    

    所以我的对话框看起来像

    enter image description here

    我解决了

    1)将导入从 android.app.AlertDialog 更改为 android.support.v7.app.AlertDialog
    2)我在 AppTheme 中使用null值覆盖2属性

    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">#FFC107</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#FFFFFF</item>
        <!-- Used for the background -->
        <item name="android:background">#4CAF50</item>
    
    
        <item name="android:textColor">@null</item>
        <item name="android:textSize">@null</item>
    </style>
    

    .

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle);
    

    希望它能帮到另一个人

    enter image description here

  • 422
    <item name="editTextColor">@color/white</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorHint">@color/gray</item>
        <item name="android:textColorPrimary">@color/gray</item>
        <item name="colorControlNormal">@color/gray</item>
        <item name="colorControlActivated">@color/white</item>
        <item name="colorControlHighlight">#30FFFFFF</item>
    

相关问题