android 开发中 对于 shape 和 selector的使用,一直都不是很熟练, 记录一下.便于以后参考.
举个项目中例子图
对于上面的2个radiobutton ,背景我们可以让美工做一个.9图来实现. 但是最后还是自己用shape 搞一下.
按钮布局如下:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/space_8" android:orientation="horizontal"> <RadioGroup android:id="@+id/order_rg" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="@dimen/space_8" android:layout_marginRight="@dimen/space_8" android:orientation="horizontal"> //默认选中 <RadioButton android:id="@+id/rb_left" android:layout_width="match_parent" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/fg_order_rb_selector" android:button="@android:color/transparent" android:checked="true" android:drawablePadding="@dimen/space_4" android:gravity="center" android:text="投资记录" android:textColor="@drawable/fg_order_text_selector" android:textSize="@dimen/txt_14" /> //默认不选中 <RadioButton android:id="@+id/rb_right" android:layout_width="match_parent" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/fg_order_rb_selector1" android:button="@null" android:drawablePadding="@dimen/space_4" android:text="提现记录" android:gravity="center" android:textColor="@drawable/fg_order_text_selector" android:textSize="@dimen/txt_14" /> </RadioGroup> </LinearLayout>
里面用到2个 selector , 1个是文字背景选择器fg_order_text_selector,1个RadioButton颜色背景选择器fg_order_rb_selector1
2个RadioButton用到的文字选择器如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/white" android:state_checked="true"/> //白色 <item android:color="@color/black" android:state_checked="false"/>//黑色 </selector>
第一个RadioButton用到的颜色背景选择器 :fg_order_rb_selector.xml , rb_left.xml , rb_right.xml 如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/rb_left" android:state_checked="true" android:state_enabled="true" /> <item android:drawable="@drawable/rb_right" android:state_checked="false" android:state_enabled="false"/> </selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 圆角 --> <corners android:bottomLeftRadius="6dp" android:topLeftRadius="6dp" /> <stroke android:color="@color/wave_bg" android:width="1dp" /> <!--内部填充色--> <solid android:color="@color/wave_bg" /> </shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 圆角 --> <corners android:bottomLeftRadius="6dp" android:topLeftRadius="6dp" /> <stroke android:color="@color/wave_bg" android:width="1dp" /> <!--内部填充色--> <solid android:color="@color/white"/> </shape>
第二个RadioButton用到的颜色背景选择器 :fg_order_rb_selector1.xml , rb_left1.xml , rb_right1.xml 如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/rb_left1" android:state_checked="true" android:state_enabled="true" /> <item android:drawable="@drawable/rb_right1" android:state_checked="false" android:state_enabled="false"/> </selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 圆角 --> <corners android:bottomRightRadius="6dp" android:topRightRadius="6dp" /> <stroke android:color="@color/wave_bg" android:width="1dp" /> <!--内部填充色--> <solid android:color="@color/wave_bg" /> </shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 圆角 --> <corners android:bottomRightRadius="6dp" android:topRightRadius="6dp" /> <stroke android:color="@color/wave_bg" android:width="1dp" /> <!--内部填充色--> <solid android:color="@color/white"/> </shape>
以上就是 全部资源文件了. 没有用到任何一张图.
参考博文:http://blog.csdn.net/zzy7075/article/details/42235947