2013-02-16 24 views
42

我在Eclipse中创建了一个新的应用程序,目标是Jelly Bean。这是所有自动创建的代码。清单设置应用程序主题AppName的:Android对话主题使图标太亮

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

翻译为AppBaseTheme在值DIR风格:

<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> 

和值-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() { 
做出此修复程序

现在对话框如下所示:

enter image description here

回答

43

问题是您正在使用私人资源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主题默认提醒图标为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();  
     } 

不过我建议从Android源的资源复制到项目的资源,因为这是你可以肯定的是,图标将总是一样的唯一途径。

+0

按照您的评论我读的信息和修复代码。我将很快更新问题以反映结果。 :::顺便说一句,我并不担心图标看起来总是一样的,只是它们是可见的。 – ilomambo 2013-02-16 14:10:35

+0

信息对话框怎么样? 是否有类似的 '.setIconAttribute(android.R.attr。????)' 到位 '.setIcon(android.R.drawable.ic_dialog_info)的使用' – JFar 2016-03-24 23:00:54

1

一个workround基于Tomik's answer预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); 
}