Android Dialog主题使图标太亮

ilo*_*mbo 45 icons android themes android-alertdialog

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    . . . 
Run Code Online (Sandbox Code Playgroud)

对于值dir中的样式,它转换为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>
Run Code Online (Sandbox Code Playgroud)

而值-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>
Run Code Online (Sandbox Code Playgroud)

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

    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();    
            }
Run Code Online (Sandbox Code Playgroud)

结果对话框是:

结果对话框

为什么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() {
Run Code Online (Sandbox Code Playgroud)

现在对话框如下所示:

在此输入图像描述

Tom*_*mik 49

问题是您正在使用私人资源android.R.drawable.ic_dialog_alert.

使用私有资源是有问题的,它们可能因不同的设备而异,您甚至无法确定它们是否存在于所有设备上.最好的办法是避免使用私人资源.如果您需要它们,您应该从Android源复制它们并将它们放入项目的资源中.

资源太白的确切原因是您使用的android.R.drawable.ic_dialog_alert是用于标准(非Holo)主题的resource().
但对于运行Android 4.x(API级别14)的设备,您使用的是Holo theme(android:Theme.Holo.Light.DarkActionBar),它通常使用不同的资源.

使用Holo主题时,默认警报图标android.R.id.ic_dialog_alert_holo_dark适用于黑暗主题或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();    
        }
Run Code Online (Sandbox Code Playgroud)

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