style的使用步骤
1.res/values文件夹下创建styles.xml
2.styles.xml中声明style标签,指定name
3.覆盖UI控件的属性,比如android:layout_width
4.在布局文件中通过style="@style/styleName"给UI控件指定样式
style的继承
如果当前的样式与其它的样式差距并不大时,存在重复性较高的属性,这时可以考虑继承,已经存在的样式可以继承自定义的style,也可以继承系统的style
1.通过ParentStyle.ChildStyle的方式
2.通过parent="ParentStyle"的方式
3.注意:在ChildStyle中可以将ParentStyle中的样式覆盖
Theme的使用步骤
1.在res/values文件夹中创建style.xml
2.使用style标签,指定name
3.覆盖UI控件的属性/某些Activity和Application的属性
4.在AndriodManifest.xml文件中通过style="自定义样式"给Application或者Acitvity指定主题
主题和样式的区别
1.主题作用于AndroidManifest.xml文件中的application和activity标签,作用于整个应用或者整个Activity;而样式都写在布局文件中的UI控件属性中,用于单个View,TextView,Button
2.主题定义的样式应该是改变窗口外观的样式,如窗口标题等
3.如果一个应用使用了主题,但是在布局文件中UI控件也指定了样式,则样式优先级高
shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 形状的颜色填充 -->
<solid android:color="@color/red"/>
<!--
gradien颜色的渐变 如果与solid一起使用,solid失效
startColor 起始的颜色值
centerColor 中间颜色值
endColor 结束颜色值
angel 颜色渐变的角度,默认从0度开始,取值一定是45的倍数
-->
<gradient
android:angle="90"
android:startColor="@color/red"
android:endColor="@color/green"/>
<!--
radius 设置四个角度的角度 如果其他四个值都同时制定,radius失效
android:topLeftRadius 设置左上角的角度
android:topRightRadius 设置右上角的角度
android:bottomLeftRadius 设置左下角的角度
android:bottomRightRadius 设置右下角的角度
-->
<corners
android:radius="30dp"
android:topLeftRadius="45dp"
android:topRightRadius="45dp"
android:bottomLeftRadius="45dp"
android:bottomRightRadius="45dp"/>
<!--
描边
android:width 设置描边的宽度
android:color 设置描边的颜色
android:dashGap 设置虚线的间隙
android:dashWidth 一个虚线的长度
-->
<stroke
android:width="2dp"
android:color="#0000FF"
android:dashGap="10dp"
android:dashWidth="4dp"/>
<size
android:width="200dp"
android:height="100dp"/>
</shape>
<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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="@drawable/btn_shape" />
</LinearLayout>
Selector状态选择器
在描述选择器的xml文件中,状态选择器是从上往下依次匹配
android:state_pressed 表示当手指触摸此UI控件时的状态,试用于ImageView和Button
android:state_checked 表示UI控件是否被选中状态时的选择器,适用于CheckBox,RadioButton
android:state_focused 表示UI控件是否聚焦时的状态选择器,适用于EditText,AutoCompleteTextView,SearchView