Define styles and themes Res/values/style. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <style name = "mystyle1"> <item name = "Android: textcolor"> # ff0000 </item> <item name = "Android: textsize "> 10sp </item> <item name =" Android: padding "> 5dip </item> </style> <style name =" mystyle2 "parent =" mystyle1 "> <! -- Override the style of the parent class --> <item name = "Android: textsize"> 20sp </item> <item name = "Android: background "> # FFFF00 </item> </style> <style name =" mytheme "> <item name =" Android: background "> @ drawable/ic_launcher </item> <item name =" Android: windownotitle "> true </item> <item name =" Android: windowfullscreen "> true </item> </style> </resources>
Res/values/string. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, styleandthemeactivity! </String> <string name = "app_name"> style and topic </string> <string name = "TV"> London Olympics </string> </resources>
Layout file layout/Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/TV" style = "@ style/mystyle1"/> <! -- Use style --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "@ string/TV" style = "@ style/mystyle2"/> <! -- Use style --> </linearlayout>
Java code:
Import android. app. activity; import android. OS. bundle; public class styleandthemeactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {// set the topic settheme (R. style. mytheme); // The code must be in super. oncreate (savedinstancestate); before/** in androidmanifest. add Android: theme = "@ style/mytheme" to the <Application> element in XML settings. This topic is used by all activities * to add android to the <activity> element: theme = "@ style/mytheme" use this topic for the specified activitie * Subject and style. The difference is that a style is used in a control, the topic is used in an activity or even the whole application */super. oncreate (savedinstancestate); setcontentview (R. layout. main );}}