我的Android进阶之旅------>Android中Dialog系统样式讲解
2015-11-13 10:30
946 查看
今天在维护公司的一个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_LIGHTAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_LIGHT)
主题为:AlertDialog.THEME_TRADITIONALAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_TRADITIONAL)
主题为:AlertDialog.THEME_DEVICE_DEFAULT_DARKAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK)
主题为:AlertDialog.THEME_DEVICE_DEFAULT_LIGHTAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
==================================================================================== 作者:欧阳鹏 欢迎转载,与人分享是进步的源泉! 转载请保留原文地址:http://blog.csdn.net/ouyang_peng====================================================================================
弹出一个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_LIGHTAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_LIGHT)
主题为:AlertDialog.THEME_TRADITIONALAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_TRADITIONAL)
主题为:AlertDialog.THEME_DEVICE_DEFAULT_DARKAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK)
主题为:AlertDialog.THEME_DEVICE_DEFAULT_LIGHTAlertDialog commedialog = new AlertDialog.Builder(
WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
==================================================================================== 作者:欧阳鹏 欢迎转载,与人分享是进步的源泉! 转载请保留原文地址:http://blog.csdn.net/ouyang_peng====================================================================================
相关文章推荐
- android ViewPager
- Android中dp,dip,sp,pt和px的区别
- android 中 系统日期时间的获取
- 开发Ios、Android程序快捷方式上显示的提示数字
- 【Odoo】开源OpenErp的系统Android方面的开发交流
- Android源码分析网址
- <android5.0>之dialog的使用
- Android 4.4 的 init 进程详解
- Android 将程序从后台转到前台
- android定位使用基站
- Android——图形系统
- Android Studio 中的 SVN
- Android 中overridePendingTransition方法的使用,来设置界面跳转的动画
- Android: 修改文件 保存 ERROR;
- Android RSA加密解密
- Android中调用外部地图程序
- android侧滑的实现
- android中简单的Handler与Message
- Android 中实现在界面上右划返回上一个界面
- Android webview开发中 js与android相互调用