效果图
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 这里通过style属性的设置将styles.xml中的布局样式和组件进行关联 --> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你好" style="@style/textView_style" mce_style="@style/textView_style" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我" style="@style/textView2_style" /> </LinearLayout>
values/styles.xml中
<?xml version="1.0" encoding="utf-8"?><resources> <style name="textView_style"> <item name="android:textColor">#FF0000</item> <item name="android:textSize">55px</item> </style> <style name="textView2_style"> <item name="android:textColor">#0033FF</item> <item name="android:textSize">11px</item> </style><!-- 这里一定要注意标签的包含情况。style是resources的子标签, main.xml中组件关联样式定义是通过style的name属性进行关联的 style内部通过item标签进行属性设置,name属性即为属性名称 --></resources>
这种设计方式就跟css和jsp的关系一样,方便对于组件的样式进行统一的管理。