Android Change holo Dialogue style

Source: Internet
Author: User

Andriod Modify the dialog box by setting the theme implementation. Some elements need to be changed by Java code, the following dialog box changes the example title of the step.

1, write Text style.

Dialog title is TextView, in Sytles.xml. For example, the following code to set your own text style:

    <style name= "Dialogwindowtitle" >        <item name= "android:textsize" >22sp</item>        <item Name= "Android:textcolor" > @color/font_dark_grey</item>    </style>
2. Set the title Theme of the dialog box.

The caption text above cannot be set directly to the caption style of the dialog box.

We also need to write a style that represents the subject of the title, where the text style of the caption is specified.

The code is as follows:

    <style name= "Dialogwindowtitle.devicedefault" >        <item name= "Android:maxlines" >1</item>        <item name= "android:scrollhorizontally" >true</item>        <item name= "Android:textappearance" >@ Style/dialogwindowtitle</item>    </style>

3. Set the theme of the dialog box.

Next. We write our dialog-box theme, where we specify the topic of the title. Because some properties are not public. So we need to inherit from one of the original style, code such as the following:

    <!--Dialog Theme--    <style name= "Theme.DeviceDefault.Dialog" parent= "@android: style/ Theme.DeviceDefault.Light.Dialog ">        <item name=" Android:windowtitlestyle "> @style/ Dialogwindowtitle.devicedefault</item>    </style>

4. Define the theme of your app.

Next. We need to use this theme in our app theme to specify our dialog box. So you need to define an app theme. The same is because many of the properties of app theme are not public (for example, the blue line below the heading below), so we're going to inherit from a native style.

Here I need to choose Theme.Holo.Light.NoActionBar according to the procedure. The code is as follows:

    <style name= "Parkingtheme" parent= "@android: Style/theme.holo.light.noactionbar" >        <item name= "Android :d ialogtheme "> @style/theme.devicedefault.dialog</item>    </style>
5. Specify the theme of the app.

As a final step, we need to specify our app theme in the Androidmanifest.xml file.

This step is very easy, just need to specify the value of Android:theme in the application tag, such as the following:

        Android:theme= "@style/parkingtheme"

It's just that it's just a matter of specifying the dialog. Suppose it is a dialog created through Alertdialog, the theme is still the original. So we still need the following steps.

6, write Alertdialog theme.

We cannot directly inherit the style of Alertdialog in the system theme.

If the parent is specified as Theme.devicedefault.dialog.alert,theme.holo.dialog.alert,theme.devicedefault.light.dialog.alert or Theme.Holo.Lig Ht. Dialog.alert, will lead to compilation just. So we need to inherit the style from dialog. Here I take Theme.Holo.Light.Dialog as an example, code such as the following:

    <!--alderdialog Theme--    <style name= "Theme.DeviceDefault.Dialog.Alert"  parent= "@android: style/ Theme.Holo.Light.Dialog ">        <item name=" Android:windowbackground "> @android: color/transparent</ item>        <item name= "Android:windowtitlestyle" > @style/dialogwindowtitle.devicedefault</item>        <item name= "Android:windowcontentoverlay" > @null </item>        <item name= "Android: Windowminwidthmajor "> @android:d imen/dialog_min_width_major</item>        <item name=" Android: Windowminwidthminor "> @android:d imen/dialog_min_width_minor</item>    </style>

Here I took the native Alertdialog style and set the background of the form to be transparent. And Windowcontentoverlay is null for both of these important properties, otherwise you will see the background of another layer of dialog below Alertdialog, or the background of the dialog box that obscures the entire contents of this problem exists.

7, specify the theme of Alertdialog.

We need to add a line of code to specify the Alertdialog style to use in the apptheme that we defined in step 4th, such as the following:

<item name= "Android:alertdialogtheme" > @style/theme.devicedefault.dialog.alert</item>
8. Change the blue line below the title.

If you change the theme color of the dialog, the blue line below the headline will surely make you very depressed. If the dialog box is small, you can choose to hide the title. Then you define a view that includes the caption to set the contents of the dialog box.

But if you have a lot of dialog box, and can be used to call the original API to generate, to define so many titles with the view, so it is certainly very tangled in the heart.

The Blue line below the title. Not defined in dialog or Alertdialog, or through their style. It is defined in the layout of various styles of dialog, and then in Apptheme the corresponding properties of the dialog are specified. It's a pity. At the moment I see these related properties are not public, can not set their own, so only through the Java code to achieve.

This blue line is called Titledivider, and we are able to get its IP through the getresources () API, and then set the color. The code is as follows:

    public static final void Dialogtitlelinecolor (Dialog Dialog, int color) {        Context context = Dialog.getcontext ();        int divierid = Context.getresources (). Getidentifier ("Android:id/titledivider", NULL, NULL);        View divider = Dialog.findviewbyid (Divierid);        Divider.setbackgroundcolor (color);    }

This line of code can be called after Setcontentview for its own defined dialog. For Alertdialog, however, you must show () the method to invoke after the call. Otherwise you will get an error.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Android Change holo Dialogue style

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.