selector:选择器,用于提示用户当前选择的界面或功能,在不同的选择状态下显示不同的图片。定义在res/drawable下的xml文件中
以前看到微信界面和qq界面时,就在想下面那按钮时怎么实现的,现在才知道,用selector实现,这么简单。就是一个没有选择和选择了两张图片的切换。
style.xml
<style name="my_dot">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:button">@null</item>
</style>
布局:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.huse.seletor1.MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFAFA" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_libao"
style="@style/my_dot"
android:drawableTop="@drawable/libao" />
<RadioButton
android:id="@+id/rb_youxi"
style="@style/my_dot"
android:drawableTop="@drawable/youxi" />
<RadioButton
android:id="@+id/rb_tese"
style="@style/my_dot"
android:drawableTop="@drawable/tese" />
<RadioButton
android:id="@+id/rb_remen"
style="@style/my_dot"
android:drawableTop="@drawable/remen" />
<RadioButton
android:id="@+id/rb_wode"
style="@style/my_dot"
android:drawableTop="@drawable/wode" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
下面资源都是selector
libao.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!--设置按钮被选中时的图片 -->
<item android:drawable="@drawable/m_libao" android:state_checked="true"></item>
<!--设置默认时候的图片 要放到最后面-->
<item android:drawable="@drawable/x_libao"></item>
</selector>
remen.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/m_iconhot" android:state_checked="true"></item>
<item android:drawable="@drawable/x_iconhot"></item>
</selector>
tese.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/m_tese" android:state_checked="true"></item>
<item android:drawable="@drawable/x_tese"></item>
</selector>
wode.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/m_wode" android:state_checked="true"></item>
<item android:drawable="@drawable/x_wode"></item>
</selector>
youxi.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/m_youxi" android:state_checked="true"></item>
<item android:drawable="@drawable/x_youxi"></item>
</selector>
运行结果截图:
上面界面就可以根据自己的需求来写啦,给下面的单选按钮添加监听就要了。