@linux1s1s
2017-01-22T08:43:06.000000Z
字数 2569
阅读 2366
AndroidDrawable 2016-06
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] > --- 默认为rectangle<corners -- shape=“rectangle”时使用,android:radius="integer" -- 半径,会被下边的属性覆盖,默认为1dp,android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer" /><gradient -- 渐变android:angle="integer"android:centerX="integer"android:centerY="integer"android:centerColor="integer"android:endColor="color"android:gradientRadius="integer"android:startColor="color"android:type=["linear" | "radial" | "sweep"]android:useLevel=["true" | "false"] /><paddingandroid:left="integer"android:top="integer"android:right="integer"android:bottom="integer" /><size -- 指定大小,一般用在imageview配合scaleType属性使用。大小一般会适配滴android:width="integer"android:height="integer" /><solid -- 填充颜色,可是是十六进制颜色。(比如想设置半透明效果,直接使用十六就只就OK)android:color="color" /><stroke -- 指定边框,border,dashWidth和dashGap有一个为0dp则为android:width="integer"android:color="color"android:dashWidth="integer" -- 虚线宽度android:dashGap="integer" /> -- 虚线间隔宽度</shape>
以上为Shape属性的详细解释,了解上面的属性以后,我们就可以实战了。
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFFFF" /><corners android:radius="50dp" /><stroke android:width="1px" android:color="#ff00ee00" /></shape>
solid 表示 填充颜色 (英文意思是 实心的, 表示填充)
corners 表示半径值 (英文意思是 圆角)
stroke 表示边框的属性,比如边框的宽度,边框的颜色 (英文意思是 在..上划线)
当然中间的Hint和搜索logo是没有的,现在如果加入当用户点击后,边框改变颜色的需求,该如何做?
很明显会在第一时间想到selector
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"><shape><solid android:color="@android:color/white"/><corners android:radius="20dp"/><stroke android:width="2dp" android:color="#00ff00"/></shape></item><item ><shape><solid android:color="@android:color/white"/><corners android:radius="20dp"/><stroke android:width="2dp" android:color="#00aa00"/></shape></item></selector>
但是有些人这么做:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FFFFFFFF" /><corners android:radius="50dp" /><stroke android:width="1px" android:color="@drawable/test_color_bg_for_shape" /></shape>
test_color_bg_for_shape.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="true" android:color="#00ff00"/><item android:state_checkable="true" android:color="#00ff00"/><item android:color="#00ee00"/></selector>
试问,这么做可以实现相同的效果吗,为什么?
