Android中Dialog系统样式讲解

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

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

Android中Dialog系统样式讲解_第1张图片

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

 Android中Dialog系统样式讲解_第2张图片

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

 

修改之前代码如下:

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this)  
  3.                     .setTitle(title)  
  4.                     .setView(vi_nolong)  
  5.                     .setPositiveButton(  
  6.                             WalkieTalkieActivity.this.getResources().getString(R.string.ok),  
  7.                             new DialogInterface.OnClickListener() {  
  8.                                 public void onClick(DialogInterface dialog, int arg1) {  
  9.                                       
  10.                                     int j = mSelectedGroupNum + 1;  
  11.                                     int power_last = mIntercomSharePrefs.getInt("CurrentPower_"+j,0);  
  12.                                     Log.i("wxj""btn_power CurrentPower_"+j+" :" + power_last);  
  13.                                     if (power_last == 1) {  
  14.                                         mEditor.putInt("CurrentPower_"+j,0).commit();  
  15.                                         mIntercom.setPowerLevel(0);  
  16.                                         btn_power.setBackgroundResource(R.drawable.power_high);  
  17.                                           
  18.                                     } else if (power_last == 0) {  
  19.                                         mEditor.putInt("CurrentPower_"+j,1).commit();  
  20.                                         mIntercom.setPowerLevel(1);  
  21.                                         btn_power.setBackgroundResource(R.drawable.power_low);  
  22.                                     }  
  23.                                     dialog.dismiss();  
  24.                                     ((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);    
  25.                                 }  
  26.                             })  
  27.                     .setNegativeButton(  
  28.                             WalkieTalkieActivity.this.getResources().getString(R.string.cancel),  
  29.                             new DialogInterface.OnClickListener() {  
  30.                                 public void onClick(DialogInterface dialog,  
  31.                                         int whichButton) {  
  32.                                       
  33.                                     dialog.dismiss();  
  34.                                     ((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);    
  35.                                 }  
  36.                             }).create();  
  37.             commedialog.setCanceledOnTouchOutside(false);  
  38.             commedialog.show();  


 

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

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(WalkieTalkieActivity.this)  


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

 

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_DARK)  

 

完整代码如下:

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_DARK)  
  3.                     .setTitle(title)  
  4.                     .setView(vi_nolong)  
  5.                     .setPositiveButton(  
  6.                             WalkieTalkieActivity.this.getResources().getString(R.string.ok),  
  7.                             new DialogInterface.OnClickListener() {  
  8.                                 public void onClick(DialogInterface dialog, int arg1) {  
  9.                                       
  10.                                     int j = mSelectedGroupNum + 1;  
  11.                                     int power_last = mIntercomSharePrefs.getInt("CurrentPower_"+j,0);  
  12.                                     Log.i("wxj""btn_power CurrentPower_"+j+" :" + power_last);  
  13.                                     if (power_last == 1) {  
  14.                                         mEditor.putInt("CurrentPower_"+j,0).commit();  
  15.                                         mIntercom.setPowerLevel(0);  
  16.                                         btn_power.setBackgroundResource(R.drawable.power_high);  
  17.                                           
  18.                                     } else if (power_last == 0) {  
  19.                                         mEditor.putInt("CurrentPower_"+j,1).commit();  
  20.                                         mIntercom.setPowerLevel(1);  
  21.                                         btn_power.setBackgroundResource(R.drawable.power_low);  
  22.                                     }  
  23.                                     dialog.dismiss();  
  24.                                     ((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);    
  25.                                 }  
  26.                             })  
  27.                     .setNegativeButton(  
  28.                             WalkieTalkieActivity.this.getResources().getString(R.string.cancel),  
  29.                             new DialogInterface.OnClickListener() {  
  30.                                 public void onClick(DialogInterface dialog,  
  31.                                         int whichButton) {  
  32.                                       
  33.                                     dialog.dismiss();  
  34.                                     ((ViewGroup) vi_nolong.getParent()).removeView(vi_nolong);    
  35.                                 }  
  36.                             }).create();  
  37.             commedialog.setCanceledOnTouchOutside(false);  
  38.             commedialog.show();  



 

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


Android中Dialog系统样式讲解_第3张图片

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

Android中Dialog系统样式讲解_第4张图片

 

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

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

[java]  view plain  copy
  1. /** 
  2.   * Constructor using a context for this builder and the {@link AlertDialog} it creates. 
  3.   */  
  4.  public Builder(Context context) {  
  5.      this(context, resolveDialogTheme(context, 0));  
  6.  }  
  7.   
  8.  /** 
  9.   * Constructor using a context and theme for this builder and 
  10.   * the {@link AlertDialog} it creates.  The actual theme 
  11.   * that an AlertDialog uses is a private implementation, however you can 
  12.   * here supply either the name of an attribute in the theme from which 
  13.   * to get the dialog's style (such as {@link android.R.attr#alertDialogTheme} 
  14.   * or one of the constants 
  15.   * {@link AlertDialog#THEME_TRADITIONAL AlertDialog.THEME_TRADITIONAL}, 
  16.   * {@link AlertDialog#THEME_HOLO_DARK AlertDialog.THEME_HOLO_DARK}, or 
  17.   * {@link AlertDialog#THEME_HOLO_LIGHT AlertDialog.THEME_HOLO_LIGHT}. 
  18.   */  
  19.  public Builder(Context context, int theme) {  
  20.      P = new AlertController.AlertParams(new ContextThemeWrapper(  
  21.              context, resolveDialogTheme(context, theme)));  
  22.      mTheme = theme;  
  23.  }  


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

 

[java]  view plain  copy
  1. static int resolveDialogTheme(Context context, int resid) {  
  2.     if (resid == THEME_TRADITIONAL) {  
  3.         return com.android.internal.R.style.Theme_Dialog_Alert;  
  4.     } else if (resid == THEME_HOLO_DARK) {  
  5.         return com.android.internal.R.style.Theme_Holo_Dialog_Alert;  
  6.     } else if (resid == THEME_HOLO_LIGHT) {  
  7.         return com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert;  
  8.     } else if (resid == THEME_DEVICE_DEFAULT_DARK) {  
  9.         return com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;  
  10.     } else if (resid == THEME_DEVICE_DEFAULT_LIGHT) {  
  11.         return com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert;  
  12.     } else if (resid >= 0x01000000) {   // start of real resource IDs.  
  13.         return resid;  
  14.     } else {  
  15.         TypedValue outValue = new TypedValue();  
  16.         context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,  
  17.                 outValue, true);  
  18.         return outValue.resourceId;  
  19.     }  
  20. }  


几个主题的值为:

[java]  view plain  copy
  1. /** 
  2.     * Special theme constant for {@link #AlertDialog(Context, int)}: use 
  3.     * the traditional (pre-Holo) alert dialog theme. 
  4.     */  
  5.    public static final int THEME_TRADITIONAL = 1;  
  6.      
  7.    /** 
  8.     * Special theme constant for {@link #AlertDialog(Context, int)}: use 
  9.     * the holographic alert theme with a dark background. 
  10.     */  
  11.    public static final int THEME_HOLO_DARK = 2;  
  12.      
  13.    /** 
  14.     * Special theme constant for {@link #AlertDialog(Context, int)}: use 
  15.     * the holographic alert theme with a light background. 
  16.     */  
  17.    public static final int THEME_HOLO_LIGHT = 3;  
  18.   
  19.    /** 
  20.     * Special theme constant for {@link #AlertDialog(Context, int)}: use 
  21.     * the device's default alert theme with a dark background. 
  22.     */  
  23.    public static final int THEME_DEVICE_DEFAULT_DARK = 4;  
  24.   
  25.    /** 
  26.     * Special theme constant for {@link #AlertDialog(Context, int)}: use 
  27.     * the device's default alert theme with a dark background. 
  28.     */  
  29.    public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;  


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

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(WalkieTalkieActivity.this)   

系统给我们的主题是:

[java]  view plain  copy
  1. TypedValue outValue = new TypedValue();  
  2.            context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,  
  3.                    outValue, true);  
  4.            return outValue.resourceId;  



 

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

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

主题为:AlertDialog.THEME_HOLO_LIGHT

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_HOLO_LIGHT)  


Android中Dialog系统样式讲解_第5张图片

 

主题为:AlertDialog.THEME_TRADITIONAL

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_TRADITIONAL)  

Android中Dialog系统样式讲解_第6张图片

 

主题为:AlertDialog.THEME_DEVICE_DEFAULT_DARK

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK)  

Android中Dialog系统样式讲解_第7张图片

 

主题为:AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

[java]  view plain  copy
  1. AlertDialog commedialog = new AlertDialog.Builder(  
  2.                     WalkieTalkieActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)  

Android中Dialog系统样式讲解_第8张图片

你可能感兴趣的:(Android中Dialog系统样式讲解)