一直习惯使用png图片进行背景设置,虽然有时候使用点9图会帮助我们建立较好的交互,但是点9图在绘制方面还是会较为繁琐,然而使用xml绘制则会更加简单,适合程序员操作,所以,如果能用xml直接配置出来的背景,就不必要麻烦点9;下面列举一些常用的背景制作;
EditText 带边框选中高亮的背景
res/drawable/edit_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="false" > <shape> <stroke android:width="2dp" android:color="#22FFFFFF" /> <corners android:radius="2dp" /> </shape> </item> <item android:state_focused="true" > <shape> <stroke android:width="2dp" android:color="#FFFFFFFF" /> <corners android:radius="2dp" /> </shape> </item> </selector>
res/drawable/edit_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="false" android:drawable="@drawable/edit_bg_off"/> <item android:state_focused="true" android:drawable="@drawable/edit_bg_on"/> </selector>
edit_bg_off.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="2dp" android:color="#22FFFFFF" /> <corners android:radius="2dp" /> </shape>
edit_bg_on.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="2dp" android:color="#FFFFFFFF" /> <corners android:radius="2dp" /> </shape>
效果:
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp" />
从上面可以看出,Selector中,android:drawable可以被完全替代成<shape/>标签
Button 文字颜色设置点击效果Selector
res/drawable/text_color_selector.xml
res/drawable/text_color_selector.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:color="@color/red"/> <item android:state_pressed="false" android:color="@color/black"/> </selector>
android:textColor="@drawable/text_color_selector"
暂时没有找到其他方法设置,有待补充;
Button 带描边,背景渐变,边角带弧度
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" 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 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> <gradient android:startColor="#ff9d77" android:endColor="#ff9d77" android:angle="270" /> <stroke android:width="2dp" android:color="#fad3cf" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
本例参考:http://www.jb51.cc/article/p-ebjiwvfv-bhg.html
纯色背景Button点击效果
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><color android:color="#FFFF0000" /> </item> <item android:state_pressed="false"><color android:color="#FF00FFFF" /> </item> </selector>
或者这样:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/blue" android:state_pressed="false"/> <item android:drawable="@color/red" android:state_pressed="true"/> </selector>
两种效果均是一样:
点击前:
点击后:
四个角不同弧度的圆角矩形
可以看上面的EditText背景设置
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp" />
本文来自CSDN博客 转载请联系作者
注明出处 http://www.jb51.cc/article/p-dszccakh-bgg.html