android基础--selector的运用

    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>

运行结果截图:






上面界面就可以根据自己的需求来写啦,给下面的单选按钮添加监听就要了。

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值