Andriod Modify the style of the dialog, you can set the theme to implement, some elements need to be modified by Java code, the following to modify the title of the dialog box to illustrate the steps.
1. Write a text style.
Dialog's title is a TextView, in Sytles.xml, add 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, we need to inherit from one of the original style, the code is as follows:
<!--Dialog Theme-- <style name= "Theme.DeviceDefault.Dialog" parent= "@android: style/ Theme.DeviceDefault.Light.Dialog "> <item name=" Android:windowtitlestyle "> @style/ Dialogwindowtitle.devicedefault</item> </style>
4. Customize the theme of the app.
Next, we need to specify our dialog box to use this theme in our app theme, so we need to define an app theme. Also, since many of the properties of app theme are not public (for example, the blue line below the heading below), we are going to inherit from a native style. Here I choose the Theme.Holo.Light.NoActionBar according to the program, 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 simple, just specify the value of Android:theme in the application tag, as follows:
Android:theme= "@style/parkingtheme"
But that's just the subject of dialog. If you created a dialog box through Alertdialog, the theme is still the same. So we also need the following steps.
7, 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, however. So we need to inherit the style from dialog. Here I take Theme.Holo.Light.Dialog as an example, the code is as follows:
<!--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 refer to the native Alertdialog style, set the window background is transparent, and the windowcontentoverlay is null these two important properties, otherwise you will see in Alertdialog below also has a layer of dialog box background, or a dialog box with a background that obscures all content.
8, specify the theme of Alertdialog.
We need to add a line of code to specify the Alertdialog style to use in the custom apptheme that is described in step 4th, with the following code:
<item name= "Android:alertdialogtheme" > @style/theme.devicedefault.dialog.alert</item>
9. Modify the Blue line below the title.
If you change the theme color of the dialog, the Blue line underneath the headline will definitely make you depressed. If the dialog box is small, you can choose to hide the title, and then customize a view that contains the title to set the contents of the dialog box. However, if you have a number of dialog boxes, and can be used to call the original API to generate, to define so many titles with the view, so it must be very tangled in the heart.
The blue lines below the headings are not defined in dialog or Alertdialog or through their style. It is defined in the layout of various styles of dialog, and then the corresponding properties of dialog are specified in the Apptheme. Unfortunately, I now see these several 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 can 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 custom dialog. For Alertdialog, however, the show () method must be called before it can be called, otherwise it will be an error.