Android对话框主题使图标过于浅色

50

我在Eclipse中创建了一个新的应用程序,目标版本为Jelly Bean。这是所有自动生成的代码。清单将应用程序主题设置为AppName:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    . . . 

这意味着在值目录中用于样式的名称是AppBaseTheme:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

而 values-v14/styles.xml 则是:

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

</resources>

在退出之前,我创建了一个确认对话框:

    case R.id.menu_quit:
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.confirm_title)
        .setMessage(R.string.confirm_text)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();    
            }

结果对话框如下所示:

resulting dialog box

为什么ic_dialog_icon图标如此浅?它几乎不可见。我正在使用所有默认设置,我没有修改主题或任何颜色,系统不应该选择与其背景更有对比度的图标吗?我该如何修复它?

使用修复进行编辑
根据Tomik提供的信息,我阅读了android.R.attr.alertDialogIcon的文档,并进行了以下修复(用setIconAttribute()替换了 setIcon()

        new AlertDialog.Builder(this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle(R.string.confirm_title)
        .setMessage(R.string.confirm_text)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

现在对话框看起来像这样:

在此处输入图像描述

2个回答

56
问题在于您正在使用私有资源 android.R.drawable.ic_dialog_alert
使用私有资源存在问题,它们可能因设备而异,并且您甚至不能确定它们是否存在于所有设备上。最好避免使用私有资源。如果您需要它们,应该从Android源中复制它们并将它们放入项目的资源中。
您的资源过于白色的确切原因是您正在使用用于标准(非Holo)主题的资源(android.R.drawable.ic_dialog_alert)。但对于运行 Android 4.x(API级别14)的设备,您正在使用 Holo 主题(android:Theme.Holo.Light.DarkActionBar),该主题通常使用不同的资源。
对于 Holo 主题,dark 主题的默认提示图标是 android.R.id.ic_dialog_alert_holo_dark,light 主题的默认提示图标是 android.R.id.ic_dialog_alert_holo_light(在您的情况下,您应该使用此资源)。
注意:自 API 级别 11 开始,有一个属性 android.R.attr.alertDialogIcon,它引用当前主题的默认警报对话框图标。在您的代码中,可以这样使用它:
case R.id.menu_quit:
    new AlertDialog.Builder(this)
    .setIconAttribute(android.R.attr.alertDialogIcon)
    .setTitle(R.string.confirm_title)
    .setMessage(R.string.confirm_text)
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }

我仍然建议将资源从Android源代码复制到您的项目资源中,因为那是确保图标始终看起来相同的唯一方法。


根据您的评论,我阅读了信息并修复了代码。我很快会更新问题以反映结果。另外,我不担心图标始终应该看起来相同,只要它们可见即可。 - ilomambo
1
信息对话框怎么办?有没有类似的 .setIconAttribute(android.R.attr.????)可以替换 .setIcon(android.R.drawable.ic_dialog_info) - JFar

2

如果您在这些设备上使用的是浅色主题,则可以基于 Tomik 的答案 实现针对 pre-HC 的解决方法:

AlertDialog.Builder builder = ...;
if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB) {
    Drawable icon = ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_alert).mutate();
    icon.setColorFilter(new ColorMatrixColorFilter(new float[] {
            -1, 0, 0, 0, 255, // red = 255 - red
            0, -1, 0, 0, 255, // green = 255 - green
            0, 0, -1, 0, 255, // blue = 255 - blue
            0, 0, 0, 1, 0     // alpha = alpha
    }));
    builder.setIcon(icon);
} else {
    builder.setIconAttribute(android.R.attr.alertDialogIcon);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接