应用于AlertDialog的样式无法正常工作 [英] Style applied to an AlertDialog not working correctly

查看:144
本文介绍了应用于AlertDialog的样式无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人要求我将应用程序中警报对话框"的外观与应用程序主题所使用的外观相匹配.
我设法将样式用作应用程序主题的一部分,从而将样式应用于应用程序中的所有警报对话框,但是在某些情况下,样式无法正确应用.

I've been asked to match the look of an Alert Dialog in our app to the one used by the app's theme.
I managed to apply a style to all Alert Dialogs in the app using it as part of the app's theme, but there are situations where the style is not applying correctly.

例如,当警报"对话框包含单选列表"作为其消息时,就会发生这种情况.
标题看起来很好,背景和按钮栏也很好,但是列表本身是有问题的.
首先,单选按钮及其文字描述为黑色,就像android使用的是默认颜色一样.

It happens for example when the Alert Dialog contains a 'Single Choice List' as its' message.
The title looks fine, so is the background and the button bar, but the list itself is problematic.
At first, the radio buttons as well as their textual description were black colored, as if android is using the default color.

通过使用以下属性,我设法以某种方式为单选按钮着色:

I somehow managed to color the radio buttons as I wish, by using these attributes:

<item name="android:colorControlNormal">@color/text_secondary</item>
<item name="android:colorControlActivated">@color/text_secondary</item>  

但是文本颜色仍然保持黑色,并且我尝试了android公开的个可能的文本颜色属性.

But the text color still remains black, and I've tried EVERY possible text color attribute exposed by android.

它看起来像这样:

现在,这是为警报"对话框定义的完整样式:

Now this is the full style defined for the Alert Dialogs:

<style name="GenericAlertDialog.Alter" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>

    <item name="android:windowTitleStyle">@style/DialogTitle</item>
    <item name="android:textColor">@color/text_secondary</item>
    <item name="android:textColorPrimary">@color/primary</item>
    <item name="android:background">@color/window_background</item>
    <item name="android:colorAccent">@color/accent</item>

    <item name="android:textColorAlertDialogListItem">@color/text_secondary</item>

    <!--<item name="android:textColorSecondary">@color/text_secondary</item>-->

    <item name="android:colorControlNormal">@color/text_secondary</item>
    <item name="android:colorControlActivated">@color/text_secondary</item>

</style>

这是我的主题定义:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="android:windowBackground">@color/window_background</item>

    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorAccent">@color/accent</item>

    <item name="android:textColorPrimary">@color/text_primary</item>
    <item name="android:textColorSecondary">@color/text_secondary</item>
    <item name="android:textColorHint">@color/text_hint</item>

    <item name="android:buttonStyle">@style/GenericButton</item>
    <item name="android:checkboxStyle">@style/GenericCheckBox</item>

    <item name="android:alertDialogTheme">@style/GenericAlertDialog</item>
    <item name="alertDialogTheme">@style/GenericAlertDialog</item>

</style>

这是我用来创建自定义警报对话框的代码:

This is the code I'm using to create a custom Alert Dialog:

AlertDialog.Builder dialogBuilder = null;
    try
    {
        dialogBuilder = new AlertDialog.Builder(i_OwnerActivity, R.style.GenericAlertDialog_Alter);
        LayoutInflater layoutInflater = i_OwnerActivity.getLayoutInflater();

        // Inflate the dialog's custom title view and set it's text to the matching one to this class
        View customTitleView = layoutInflater.inflate(R.layout.dialog_title, null);
        TextView customTitleTextView = (TextView) customTitleView.findViewById(R.id.DialogTitleText);

        // Set text of customTitleView

        dialogBuilder.setCustomTitle(customTitleView);

        // Create an event handler for clicking on the negative button
        dialogBuilder.setNegativeButton(R.string.action_dialog_negative_cancel, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface i_Dialog, int i_Which)
            {
                // Do Something
            }
        });
    } catch (Exception e)
    {
        LogUtils.logException(AlterDialogUtils.class, e);
    }
    return dialogBuilder;

最后,这是我用来创建带有单选列表"的警报对话框的代码:

And finally, here's the code I'm using to create an Alert Dialog with a 'Single Choice List':

dialogBuilder.setSingleChoiceItems(R.array.squelch_modes, m_InitialState, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // Do Something
            }
        });

我做错了什么?如何更改文本的颜色?

What am I doing wrong? How can I change the color of the text?

值得一提的是,我正在使用AppCompat的AlertDialog.

It is also worth saying that I'm using AppCompat's AlertDialog.

推荐答案

只是用Google找到了这个旧帖子,但是由于没有答案,我将添加针对我的情况的窍门:

Just found this old post with Google, but since there is no answer I will add what did the trick in my case:

alertdialog style

<item name="textColorAlertDialogListItem">@color/text_secondary</item>

我想这是因为父母是AppCompat theme,但是我对此不确定.我仍然在我的风格中都添加了(with and without prefix) ...

I guess that's because of the parent being an AppCompat theme, but I am not sure about this. I still added both (with and without prefix) in my style...

这篇关于应用于AlertDialog的样式无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆