偶尔用到文本框,编辑框,按钮等需要加上纯色的圆角矩形背景,这个时候可以使用shape来定义各种形状。shape可以定义:矩形 rectangle;椭圆 oval;线 line;环 ring。可以在selector,layout等里面使用,有6个子标签。可以在drawable下new Android XML File,Root Element 选择shape。
stroke标签:
color:边框颜色
width:边框宽度
dashWidth:虚线框的宽度
dashGap:虚线之间的空隙的宽度
其中,当dashGap=0dp时,为实线
关于4.0以上设备虚线会变实线的问题,网上搜索解决方案为:可以把硬件加速关了
line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
android:layerType="software"
solid属性:
color:填充颜色
corners属性:
radius:四个角的半径
topRightRadius:右上角的半径
bottomLeftRadius:右下角的半径
topLeftRadius:左上角的半径
bottomRightRadius:左下角的半径
gradient属性:
startColor:其实颜色
centerColor:中间颜色
endColor:结束颜色
centerX:中间颜色的相对X坐标(0 -- 1)
centerY:中间颜色的相对Y坐标(0 -- 1)
useLevel:(true/false), 是否用作LevelListDrawable的标志
angle是渐变角度,必须为45的整数倍。0从左到右,90从下到上,180从右到左,270从上到下
type:渐变模式。 默认线性渐变,可以指定渐变为radial(径向渐变)或者sweep(类似雷达扫描的形式)
gradientRadius:渐变半径,径向渐变需指定半径。
padding属性:
left:左内边距
top:上内边距
right:右内边距
bottom:下内边距
size属性:
width:宽
height:高
效果图:
圆角虚线框:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- <solid android:color="#7bc3fd" /> -->
<!-- 圆角 -->
<corners android:radius="10dp" />
<!-- 虚线框 -->
<stroke
android:dashGap="3dp"
android:dashWidth="6dp"
android:width="2dp"
android:color="#7bc3fd" />
</shape>
虚线:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<size android:height="2dp"/>
<stroke
android:dashGap="3dp"
android:dashWidth="6dp"
android:width="2dp"
android:color="#7bc3fd" />
</shape>
圆角渐变框:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:radius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<gradient
android:angle="270"
android:endColor="#1c1cea"
android:startColor="#bebeea" />
</shape>