以前做Launcher的时候是采用3D引擎,对于UI好多界面都是靠代码布局和绘制,基本上很怎么关注过style和theme(xml)的自定义。现在用到了基本上是从头学习和总结了一番,对多数人来说可能比较初级,blog之简单mark下。干货:
一些多处用到的style,比如定义一个统一风格的Button(其他控件都一样)。
一个布局xml中正常普通的Button:
<Button android:layout_width="fill_parent" android:layout_height="@dimen/st_button_normal_height" android:layout_gravity="center" android:background="@drawable/st_button_normal_bg" android:textColor="@color/st_button_normal_textcolor" android:textSize="@dimen/st_button_normal_textsize" android:layout_marginTop="@dimen/activity_vertical_margin" android:text="@string/st_create_activity_create" />提取红色部分项到style.xml成为自定义style:
<style name="NormalButton"> <item name="android:layout_gravity">center</item> <item name="android:background">@drawable/st_button_normal_bg</item> <item name="android:textColor">@color/st_button_normal_textcolor</item> <item name="android:textSize">@dimen/st_button_normal_textsize</item> </style>在布局xml的Button中使用:
<Button style="@style/NormalButton" android:layout_width="fill_parent" android:layout_height="@dimen/st_button_normal_height" android:layout_marginTop="@dimen/activity_vertical_margin" android:text="@string/st_create_activity_create" />style可继承,可继承自己定义的style,也可以继承Android提供的style:
<style name="CustomButton" parent="NormalButton"> <item name="android:layout_height">wrap_content</item> </style>自定义一个theme:
<color name="custom_theme_color">#b0b0ff</color> <style name="CustomTheme" parent="android:Theme.Light"> <item name="android:windowBackground">@color/custom_theme_color</item> <item name="android:colorBackground">@color/custom_theme_color</item> </style>这里继承了系统的Theme.Light主题,并定义了两个属性,还有其他很多属性标签,自己按需定义,继承的parent自己也可以按需选择。 使用自定义的全局theme:
在AndroidManifest.xml的Application标签中添加:
<application android:theme="@style/CustomTheme">这样便定义了全局的theme,假如个别的activity想使用其他theme怎么办?其实activity标签也支持设定一个theme,只要在Activity标签中添加同样一句代码即可,并且Android提供了很多的Theme可供选择:
<activity android:theme="@android:style/Theme.Dialog">因为涉及到Android版本支持的问题,这个略麻烦一点,不过官方和第三方库都有解决方案。 对于Android3.0及之上,自定义theme的时候,设置actionBarStyle即可:
res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> </style> </resources>然后像二中那样设置application的theme即可。 对于2.1~3.0(不含)之间的版本可以使用android的support库v7-appcompat进行ActionBar的支持:
res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> <!-- Support library compatibility --> <item name="actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> <!-- Support library compatibility --> <item name="background">@drawable/actionbar_background</item> </style> </resources>注意红字部分(<!– Support library compatibility –>)必须添加,不然自定义不会生效。
另外是如果使用v7-appcompat库进行ActionBar的支持,则自定义theme必须继承自Theme.AppCompat(或继承自这个theme的其他Android提供的Theme),否则会报错。
一~三基本够用了,需要定义什么属性自己按需就好了。
转自:http://blog.kainaodong.com/?p=17
