First, let's determine what the style and theme are?
We all know that we can. in XML or other layout files, you can set the style of the control through attribute settings, which basically requires you to set attributes for each control. what's even more annoying is that, many of these attributes are the same!
In the era when programmers look forward to the emergence of the Savior every day, the sky is thunderous, style and theme come out of thin air, helping us solve this big problem. Style allows us to put some public attribute settings together and define a style name. When we want to use these settings in the control, we only need to call this style. As for theme, you can think of it as a special style.
1) style is generally used to control the Property Control of controls.
2) theme is generally used to control window attribute settings
--------------------------------------------------------------------
The following describes the specific application methods:
Question 1: Where are our style settings?
All our styles (including theme) are stored in RES/values /*. in XML, the name of the XML file can be customized, But we generally use style for standardization. XML or styles. XML
Style sample code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "textview">
<Item name = "Android: textsize"> 18sp </item>
<Item name = "Android: textcolor"> #008 </item>
<Item name = "Android: shadowcolor"> @ Android: color/Black </item>
<Item name = "Android: shadowradius"> 2.0 </item>
</Style>
<Style name = "edittext">
<Item name = "Android: shadowcolor"> @ Android: color/Black </item>
<Item name = "Android: shadowradius"> 1.0 </item>
<Item name = "Android: background"> @ Android: drawable/btn_default </item>
<Item name = "Android: textappearance">? Android: ATTR/textappearancemedium </item>
</Style>
<Style name = "button">
<Item name = "Android: background"> @ Android: drawable/edit_text </item>
<Item name = "Android: textappearance">? Android: ATTR/textappearancemedium </item>
</Style>
</Resources>
XML defines three styles. When we call them in a program, we can call them through the name attribute value of the style. (Note: we have mentioned style. the XML name can be defined at will, but the structure of the label in it is not random. To define the style, you must define the style label under the resources label. The sub-attributes of the content can be set through item)
Let's take a look at a theme example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "theme" parent = "Android: theme. Light">
<Item name = "Android: windowfullscreen"> true </item>
<Item name = "Android: windowtitlesize"> 160dip </item>
<Item name = "Android: windowtitlestyle"> @ style/windowtitle </item>
</Style>
<Style name = "windowtitle" parent = "Android: windowtitle">
<Item name = "Android: singleline"> true </item>
<Item name = "Android: shadowcolor"> # bb000000 </item>
<Item name = "Android: shadowradius"> 5.75 </item>
</Style>
</Resources>
No, the same structure! That's why theme is a special style.
-----------------------------------------------------------------
Let's take a look at how to call the defined style. Assume that we have a style related to edittext. The name attribute of this style is style_edittext, and there is a theme related to window settings. The theme name is theme_ OK. How can we use them?
First, use style_edittext for a specific edittext.
<Edittext
Android: Id = "@ + ID/myedittext"
Android: layout_width = "fill_parent"
Android: layout_height = "Wrap + parent"
Style = "@ style/style_edittext"
Android: Hit = "Welcome to chenzheng_java blog"
/>
Second: Use theme_ OK in the program
Public class themestyleactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settheme (R. style. theme_ OK); <br/> setcontentview (R. layout. main); <br/>}< br/>
We can see that we implement it through the settheme method.
Third: Add a topic for the entire application or activity.
To add a topic for the entire application, you can add the Android: Theme attribute for the <Application> tag in androidmanifest. xml. For example, Android: theme = "@ style/theme_ OK"
To add a topic for an activity, you can add the Android: Theme attribute for the <activity> tag in androidmanifest. xml. For example, Android: theme = "@ style/theme_ OK"
---------------------------------------------------------------------------