My Android Advanced Tour------>android Dialog System style explained

Source: Internet
Author: User

Today, while maintaining an app for the company, there are a few scenarios.

Pop up a alertdialog when the system language is Chinese, as follows:

Pop up a alertdialog when the system language is 中文版, as follows:

It can be found that when the system language is English, the white text in the dialog box is completely unclear, and the background color of the dialog box becomes white. Therefore, you need to modify the theme of the dialog box.

The previous code was modified as follows:

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;in T 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 ();


It can be found that the new Alertdialog.builder did not specify a theme,

Alertdialog Commedialog = new Alertdialog.builder (walkietalkieactivity.this)


We can specify a topic at new Alertdialog.builder, as follows:

Alertdialog Commedialog = new Alertdialog.builder (Walkietalkieactivity.this,alertdialog.theme_holo_dark)

The complete code is as follows:

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 ();



This specifies a black background theme, so the background is black when the system language is English, as shown below:


When the system language is Chinese, the background is also black, as shown below:

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

Below from the source point of view to see what is going on, look at the Alertdialog.build code as follows:

       /** * 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 CRE  Ates. The actual theme * That's Alertdialog uses is a private implementation and however you can * here supply EIT Her the name of a 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.them E_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) code is as follows:

    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_di        Alog_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; }    }


The values for several topics are:

 /** * Special theme constant for {@link #AlertDialog (Context, int)}: Use * the traditional (Pre-holo) alert Dialo     G 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 Ligh     T 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; 


Thus, when we do not specify the subject,

The system gives us the following themes:

Typedvalue outvalue = new Typedvalue ();            Context.gettheme (). Resolveattribute (Com.android.internal.r.attr.alertdialogtheme,                    outValue, true);            return Outvalue.resourceid;



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

Here are some of the topics to test

The theme is: Alertdialog.theme_holo_light

Alertdialog Commedialog = new Alertdialog.builder (walkietalkieactivity.this,alertdialog.theme_holo_light)


The theme is: alertdialog.theme_traditional

Alertdialog Commedialog = new Alertdialog.builder (walkietalkieactivity.this,alertdialog.theme_traditional)

The theme is: Alertdialog.theme_device_default_dark

Alertdialog Commedialog = new Alertdialog.builder (Walkietalkieactivity.this,alertdialog.theme_device_default_dark)

The theme is: Alertdialog.theme_device_default_light

Alertdialog Commedialog = new Alertdialog.builder (walkietalkieactivity.this,alertdialog.theme_device_default_light )



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

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

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





Copyright NOTICE: This article is Ouyangpeng original article, welcome reprint, reprint please indicate source! Http://blog.csdn.net/ouyang_peng

My Android Advanced Tour------>android Dialog System style explained

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.