We tasted the need to use properties such as SetText, SetColor, Settextsize, and so on to set the style of the control, but each control needed to set these properties, the workload was undoubtedly huge, and later maintenance was inconvenient.
Style
The style (style) in Android contains a set of formatting that, when used with a style for a component, all the formatting that it contains will be applied to that component.
The Android style resource file is placed in the/res/values directory, which is <resources> with the element, which contains multiple <style> child elements, each of which is a style.
The <style> element specifies the following two properties:
1.name: Specify style name
2.parent: Specifies the parent style that the style inherits from
Each <style> element contains more than one <item> child element, each of which defines a format.
/res/values/styles.xml
<Resourcesxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--Base application theme, dependent on API level. This theme are replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. - <stylename= "Appbasetheme"Parent= "Android:Theme.Light"> <!--Theme customizations available in newer API levels can go in res/values-vxx/styles.xml, while C Ustomizations related to Backward-compatibility can go. - </style> <!--application theme. - <stylename= "Apptheme"Parent= "Appbasetheme"> <!--All customizations, that is, specific to a particular api-level can go. - </style> <stylename= "TextView"> <Itemname= "Android:background">#00ff00</Item> <Itemname= "Android:textsize">30dp</Item> <Itemname= "Android:textcolor">#ffff00</Item> </style> <stylename= "TextView2"Parent= "@style/textview"> <Itemname= "Android:textcolor">#0f0f0f</Item> <Itemname= "Android:layout_margintop">10dp</Item> </style></Resources>
Activity_main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.styleandtheme.MainActivity" > <TextViewstyle= "@style/textview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "This is a Text" /> <TextViewstyle= "@style/textview2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "This is a Text" /></LinearLayout>
Operation Result:
Theme
Theme and style are very similar, also defined in the values directory, also with <resource> as the root element, the same as the <style> definition of the theme, the theme also contains <item> each item represents a style
Unlike style, theme does not work on a single component, it works for the entire activity, and theme defines the format of changing the appearance of the window, such as the window caption, window border, and so on.
/res/values/styles.xml
<Resourcesxmlns:android= "Http://schemas.android.com/apk/res/android"> <stylename= "Appbasetheme"Parent= "Android:Theme.Light"></style> <stylename= "Apptheme"Parent= "Appbasetheme"></style> <stylename= "TextView"> <Itemname= "Android:background">#00ff00</Item> <Itemname= "Android:textsize">30dp</Item> <Itemname= "Android:textcolor">#ffff00</Item> </style> <stylename= "TextView2"Parent= "@style/textview"> <Itemname= "Android:textcolor">#0f0f0f</Item> <Itemname= "Android:layout_margintop">10dp</Item> </style> <stylename= "MyTheme"> <Itemname= "Android:windownotitle">True</Item> <Itemname= "Android:windowfullscreen">True</Item> </style></Resources>
Mainactivity.java
Package Com.example.styleandtheme; Import android.app.Activity; Import Android.os.Bundle; Public class extends Activity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { super. OnCreate (savedinstancestate); SetTheme (r.style.mytheme); Setcontentview (R.layout.activity_main); }}
Operation Result:
Android Notes (72) style and theme