今天在维护公司的一个APP的时候,有如下场景。

弹出一个AlertDialog的时候,在系统语言是中文的时候,如下所示:

弹出一个AlertDialog的时候,在系统语言是English的时候,如下所示:

可以发现在系统语言为英语的时候,对话框中的白色文字已经完全看不清楚,对话框的背景颜色也变成了白色。因此需要修改对话框的主题。

修改之前代码如下:

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this)
.setTitle(title)
.setView(vi_nolong)
.setPositiveButton(
WalkieTalkieActivity.this.getResources().getString(R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) { int j = mSelectedGroupNum + 1;
int power_last = mIntercomSharePrefs.getInt("CurrentPower_"+j,0);
Log.i("wxj", "btn_power CurrentPower_"+j+" :" + power_last);
if (power_last == 1) {
mEditor.putInt("CurrentPower_"+j,0).commit();
mIntercom.setPowerLevel(0);
btn_power.setBackgroundResource(R.drawable.power_high); } else if (power_last == 0) {
mEditor.putInt("CurrentPower_"+j,1).commit();
mIntercom.setPowerLevel(1);
btn_power.setBackgroundResource(R.drawable.power_low);
}
dialog.dismiss();
((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);
}
})
.setNegativeButton(
WalkieTalkieActivity.this.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) { dialog.dismiss();
((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);
}
}).create();
commedialog.setCanceledOnTouchOutside(false);
commedialog.show();

可以发现,new AlertDialog.Builder的时候没有指定主题,

AlertDialog commedialog = new AlertDialog.Builder(WalkieTalkieActivity.this)

我们可以在new AlertDialog.Builder的时候指定一个主题,如下所示:

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_DARK)

完整代码如下:

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_DARK)
.setTitle(title)
.setView(vi_nolong)
.setPositiveButton(
WalkieTalkieActivity.this.getResources().getString(R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) { int j = mSelectedGroupNum + 1;
int power_last = mIntercomSharePrefs.getInt("CurrentPower_"+j,0);
Log.i("wxj", "btn_power CurrentPower_"+j+" :" + power_last);
if (power_last == 1) {
mEditor.putInt("CurrentPower_"+j,0).commit();
mIntercom.setPowerLevel(0);
btn_power.setBackgroundResource(R.drawable.power_high); } else if (power_last == 0) {
mEditor.putInt("CurrentPower_"+j,1).commit();
mIntercom.setPowerLevel(1);
btn_power.setBackgroundResource(R.drawable.power_low);
}
dialog.dismiss();
((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);
}
})
.setNegativeButton(
WalkieTalkieActivity.this.getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) { dialog.dismiss();
((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);
}
}).create();
commedialog.setCanceledOnTouchOutside(false);
commedialog.show();

这样的话就指定了一个黑色背景的主题,这样在系统语言为英语的时候,背景也是黑色的,如下所示:

在系统语言为中文的时候,背景也是黑色的,如下所示:

====================================================================================================================================

下面从源码角度来看看到底是怎么回事,查看AlertDialog.Build代码如下:

       /**
* Constructor using a context for this builder and the {@link AlertDialog} it creates.
*/
public Builder(Context context) {
this(context, resolveDialogTheme(context, 0));
} /**
* Constructor using a context and theme for this builder and
* the {@link AlertDialog} it creates. The actual theme
* that an AlertDialog uses is a private implementation, however you can
* here supply either the name of an attribute in the theme from which
* to get the dialog's style (such as {@link android.R.attr#alertDialogTheme}
* or one of the constants
* {@link AlertDialog#THEME_TRADITIONAL AlertDialog.THEME_TRADITIONAL},
* {@link AlertDialog#THEME_HOLO_DARK AlertDialog.THEME_HOLO_DARK}, or
* {@link AlertDialog#THEME_HOLO_LIGHT AlertDialog.THEME_HOLO_LIGHT}.
*/
public Builder(Context context, int theme) {
P = new AlertController.AlertParams(new ContextThemeWrapper(
context, resolveDialogTheme(context, theme)));
mTheme = theme;
}

resolveDialogTheme(Context context, int resid) 代码如下:

    static int resolveDialogTheme(Context context, int resid) {
if (resid == THEME_TRADITIONAL) {
return com.android.internal.R.style.Theme_Dialog_Alert;
} else if (resid == THEME_HOLO_DARK) {
return com.android.internal.R.style.Theme_Holo_Dialog_Alert;
} else if (resid == THEME_HOLO_LIGHT) {
return com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert;
} else if (resid == THEME_DEVICE_DEFAULT_DARK) {
return com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
} else if (resid == THEME_DEVICE_DEFAULT_LIGHT) {
return com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
} else if (resid >= 0x01000000) { // start of real resource IDs.
return resid;
} else {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,
outValue, true);
return outValue.resourceId;
}
}

几个主题的值为:

 /**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the traditional (pre-Holo) alert dialog theme.
*/
public static final int THEME_TRADITIONAL = 1; /**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the holographic alert theme with a dark background.
*/
public static final int THEME_HOLO_DARK = 2; /**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the holographic alert theme with a light background.
*/
public static final int THEME_HOLO_LIGHT = 3; /**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the device's default alert theme with a dark background.
*/
public static final int THEME_DEVICE_DEFAULT_DARK = 4; /**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the device's default alert theme with a dark background.
*/
public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;

由此可见,当我们不指定主题的时候,

AlertDialog commedialog = new AlertDialog.Builder(WalkieTalkieActivity.this) 

系统给我们的主题是:

 TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,
outValue, true);
return outValue.resourceId;

====================================================================================================================================

下面分别来测试一下这几个主题

主题为:AlertDialog.THEME_HOLO_LIGHT

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_LIGHT)

主题为:AlertDialog.THEME_TRADITIONAL

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_TRADITIONAL)

主题为:AlertDialog.THEME_DEVICE_DEFAULT_DARK

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK)

主题为:AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

AlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)

====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================

我的Android进阶之旅------>Android中Dialog系统样式讲解的更多相关文章

  1. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  2. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  3. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

  4. 我的Android进阶之旅------>Android中AsyncTask源码分析

    在我的<我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例>文章中,先后使用了Handler和AsyncTask两种方式实现异步任务机制. ...

  5. 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  6. 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计

    要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...

  7. 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现

    我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...

  8. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)

    在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...

  9. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)

    正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...

随机推荐

  1. 【MyBatis学习09】高级映射之一对多查询

    上一篇博文总结了一下一对一的映射,本文主要总结一下一对多的映射,从上一篇文章中的映射关系图中可知,订单项和订单明细是一对多的关系,所以本文主要来查询订单表,然后关联订单明细表,这样就有一对多的问题出来 ...

  2. svn your working copy appears to be locked run cleanup to amend the situation

    cleanup  则解决

  3. CSS 温故而知新 background常用属性

    1.background-repeat 不用说,常用直接no-repeat 2.background-size 常用的分为两个,一个是铺满:cover, 另一个是使图像适应宽高:contain 3.b ...

  4. ant用途及简单实现

    ant用途及简单实现 标签: antjavadeletejarbuildjavaee 2012-07-17 14:15 5945人阅读 评论(0) 收藏 举报  分类: other(6)  Ant工具 ...

  5. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  6. 李洪强经典面试题51-KVO-KVC

    李洪强经典面试题51-KVO-KVC   KVC-KVO KVC的底层实现? 当一个对象调用setValue方法时,方法内部会做以下操作: ①检查是否存在相应key的set方法,如果存在,就调用set ...

  7. Unix 环境高级编程

    UNIX 环境高级编程 本书描述了UNIX系统的程序设计接口--系统调用接口和标准C库提供的很多函数. 与大多数操作系统一样,Unix为程序员运行提供了大量的服务--打开文件,读文件,启动一个新程序, ...

  8. windows 8.1 安装 .Net Framework 3.5

    1.挂载IOS虚拟光驱 2.命令提示符(管理员)   dism.exe /online /enable-feature /featurename:NetFX3 /Source:F:\sources\s ...

  9. python第二周数据类型 字符编码 文件处理

    第一数据类型需要学习的几个点: 用途 定义方式 常用操作和内置的方法 该类型总结: 可以存一个值或者多个值 只能存储一个值 可以存储多个值,值都可以是什么类型 有序或者无序 可变或者不可变 二:数字整 ...

  10. oracle数据库中VARCHAR2(50 CHAR) 和VARCHAR2(50) 有啥区别?

    VARCHAR2(50 char)这种类型的字段最多放50个字符,不够50个用空格填充:而VARCHAR2(50)最大允许存放50个字符,但是不足50个也不用空格填充.varchar2是变长字符串,与 ...