我尽量不打错别字,用词准确,不造成阅读障碍。
shape和selector已经使用过很多次了,但是一直没在脑海中有个全面的知识网络,特此总结一下。
shape
shape属性有很多,主要介绍常用的方法。
shape常用标签:shape
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle | oval | ring | line"> //分别为"矩形","椭圆","环形","线性"
......
</shape>
其中椭圆是可以用来画圆的,只需要加size标签就行。
shape有常用的几个子标签:
*<corners .../>
: 四个角的弧度。
* <gradient.../>
: 渐变。
* <padding.../>
: 内间距。
* <size.../>
: 尺寸。
* <solid.../>
: 填充色,上什么颜色。
*<stroke.../>
: 边框。
rectangle:矩形
效果图(左侧为设置为TextView的background后,右侧为预览图):
代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> //设置为矩形
<!--设置四角的弧度都为为2dp,也可以单独设置,android:bottomRightRadius="2dp"等-->
<corners android:radius="2dp" />
<!--设置填充色-->
<solid android:color="@color/colorPrimary" />
<!--设置图形的大小,实际受制于控件的大小-->
<size
android:width="100dp"
android:height="150dp" />
<!--设置边框大小及颜色-->
<stroke
android:width="2dp"
android:color="#000000" />
<!--设置渐变色,起始颜色和末尾颜色,默认为线性渐变-->
<gradient
android:endColor="@color/colorPrimary"
android:startColor="@color/colorAccent" />
<!--设置内间距属性,在TextView中设置文字时可以显示出效果-->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
其中padding的作用是不能在右边直观看出来的,文字调大了在布局窗口就会看出来。
oval:椭圆
椭圆主要是为了画圆,只要设置一下Size标签就好了。
这时设置”corners”标签是没有用的。
效果图(左侧为设置为TextView的background后,右侧为预览图):
代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> //设置为椭圆
<solid android:color="@color/colorPrimary" />
<!--设置图形的大小,实际受制于控件的大小,通过这个标签显示成圆-->
<size
android:width="100dp"
android:height="100dp" />
<stroke
android:width="2dp"
android:color="#000000" />
<gradient
android:endColor="@color/colorPrimary"
android:startColor="@color/colorAccent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
ring:环形
效果图(左侧为设置为TextView的background后,右侧为预览图):
代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="3" //设置内环半径为整个可显示区域的1/3,默认是3
android:shape="ring" //设置为环
android:thicknessRatio="9" //设置环的宽度为整个可显示区域的1/9,默认是9
android:useLevel="false"> //设置为false环才会显示
<solid android:color="@color/colorPrimary" />
<gradient
android:endColor="@color/colorPrimary"
android:startColor="@color/colorAccent"
android:type="linear" //设置渐变类型为线性渐变
android:useLevel="false" />
</shape>
环的属性比较多,使用灵活多变,文章最后我会发一个统一的属性代码,并写上注释。
线性
效果图(左侧为设置为TextView的background后,右侧为预览图):
代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="10dp"
android:color="#ff0000"
android:dashGap="10dp" //设置线条宽度为10dp
android:dashWidth="20dp" /> //设置间距的宽度为20dp
<size android:height="3dp"/>
</shape>
注意:线性除了间距的特点外,其实有时候可以被矩形取代,比如渐变色的线。
统一的属性展示:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="false|true" //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。
android:shape="rectangle|line|oval|ring" //分别为矩形、线、椭圆、环形,默认为矩形rectangle
android:innerRadius="integer" //环形时可用,内环半径
android:innerRadiusRatio="float" //环形时可用,内环的宽度比,即环的宽度比表示内环半径,默认为3,会被innerRadius值覆盖
android:thickness="integer" //环形时可用,环的宽度
android:thicknessRatio="float" //环形时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
android:tint="color" // 给shape上色,会覆盖<solid/>标签
android:tintMode="src_in|src_atop|src_over|add|multiply|screen" //着色类型,没有研究
android:useLevel="false|true" //一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
android:visible="false|true"
>
<!-- 圆角 -->
<corners
android:radius="integer" // 圆角半径,该值设置时下面四个属性失效
android:bottomLeftRadius="integer" // 左下角圆角半径
android:bottomRightRadius="integer" // 右下角圆角半径
android:topLeftRadius="integer" // 左上角圆角半径
android:topRightRadius="integer" // 右上角圆角半径
/>
<!-- 渐变 -->
<gradient
android:useLevel="false|true" // 与上面shape中该属性的一致
android:type="linear|radial|sweep" // 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear
android:angle="integer" // 渐变角度,为线性渐变时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
android:centerColor="color" // 渐变中间位置颜色
android:startColor="color" // 渐变开始位置颜色
android:endColor="color" // 渐变结束位置颜色
android:centerX="float" // type为放射性渐变时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
android:centerY="float" // type为放射性渐变时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
android:gradientRadius="integer" // type为放射性渐变时有效,渐变的半径
/>
<!-- 内边距 -->
<padding
android:bottom="integer" // 设置底部内边距
android:left="integer" // 左边内边距
android:right="integer" // 右边内边距
android:top="integer" // 顶部内边距
/>
<!-- 大小 -->
<size
android:height="integer" // 宽度
android:width="integer" // 高度
/>
<!-- 填充 -->
<solid
android:color="color" // shape的填充色
/>
<!-- 描边 -->
<stroke
android:color="color" // 边框的颜色
android:width="integer" // 边框的宽度
android:dashGap="integer" // 虚线间隔
android:dashWidth="integer" // 虚线宽度
/>
</shape>
参考文章(感谢):
http://blog.csdn.net/qq_15128547/article/details/56680494
https://www.jianshu.com/p/ef734937b521