1style样式的目的:把相同的样式抽取出来 便于统一修改
<style name="text_content_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:text">我是文本</item>
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">22sp</item>
</style>
style节点的name自定义 要见名知意
item name和值 和 普通布局文件的键+值对应
2样式的继承:
示例
<style name="text_content_style_dark" parent="@style/text_content_style">
<item name="android:textColor">#88ff0000</item>
</style>
3继承上面的样式 并重写textColor
样式的优先级:样式的优先级比较低 控件调用样式后如果在控件内继续设置属性 属性会覆盖原来样式的属性
可以用属性值覆盖原来的样式
4样式的调用
布局文件中
<TextView style="@style/text_content_style" />
<TextView style="@style/text_content_style_dark" />
5demo
a定义样式:res/values/style
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="text_content_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:text">我是文本</item>
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">22sp</item>
</style>
<style name="text_content_style_dark" parent="@style/text_content_style">
<item name="android:textColor">#88ff0000</item>
</style>
</resources>
b.调用样式
<LinearLayout xmlns: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"
tools:context=".MainActivity" >
<TextView style="@style/text_content_style" />
<TextView style="@style/text_content_style_dark" />
<TextView
style="@style/text_content_style_dark"
android:textColor="#000000" />
</LinearLayout>