实例详解Android Selector和Shape的用法

2019-12-10 19:13:27于丽

这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

padding:间隔

button_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector 
xmlns:android="http://www.easck.com/apk/res/android"> 
<item android:state_pressed="true" > 
<shape> 
<!-- 渐变 --> 
<gradient 
android:startColor="#ff8c00" 
android:endColor="#FFFFFF" 
android:type="radial" 
android:gradientRadius="50" /> 
<!-- 描边 --> 
<stroke 
android:width="2dp" 
android:color="#dcdcdc" 
android:dashWidth="5dp" 
android:dashGap="3dp" /> 
<!-- 圆角 --> 
<corners 
android:radius="2dp" /> 
<padding 
android:left="10dp" 
android:top="10dp" 
android:right="10dp" 
android:bottom="10dp" /> 
</shape> 
</item> 
<item android:state_focused="true" > 
<shape> 
<gradient 
android:startColor="#ffc2b7" 
android:endColor="#ffc2b7" 
android:angle="270" /> 
<stroke 
android:width="2dp" 
android:color="#dcdcdc" /> 
<corners 
android:radius="2dp" /> 
<padding 
android:left="10dp" 
android:top="10dp" 
android:right="10dp" 
android:bottom="10dp" /> 
</shape> 
</item> 
<item> 
<shape> 
<solid android:color="#ff9d77"/> 
<stroke 
android:width="2dp" 
android:color="#fad3cf" /> 
<corners 
android:topRightRadius="5dp" 
android:bottomLeftRadius="5dp" 
android:topLeftRadius="0dp" 
android:bottomRightRadius="0dp" 
/> 
<padding 
android:left="10dp" 
android:top="10dp" 
android:right="10dp" 
android:bottom="10dp" /> 
</shape> 
</item> 
</selector> 

运行效果如下图:

一般状态:

实例详解Android Selector和Shape的用法