Android中使用RadioButton实现QQ底部图片切换

来源:互联网 发布:xp画图软件 编辑:程序博客网 时间:2024/06/05 02:15

使用xml进行切换,在layout中线写好布局,



然后桌布实现效果。

在drawable中建立颜色筛选器的xml文件向其中添加如下代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_checked="true" android:drawable="@drawable/a2"></item>
<item android:state_checked="false" android:drawable="@drawable/a1"></item>
 
</selector>

此代码实现了颜色的click事件true或者false进行颜色切换,然后在布局代码中将筛选器作为图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/co">
        <View android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/col"
            android:background="#000"/>
    </LinearLayout>
    <View android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ccc"/>
    <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:id="@+id/r"
           android:orientation="horizontal">
           <RadioButton android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="消息"
               android:button="@null"
               android:id="@+id/r1"
               android:layout_gravity="center_horizontal"
               android:drawableTop="@drawable/picture"           //其中picture为图片筛选器的xml文件
               android:checked="true"
               android:gravity="center_horizontal"
               android:layout_weight="1"/>
            <RadioButton android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:drawableTop="@drawable/picture1"
               android:text="联系人"
               android:button="@null"
                android:id="@+id/r2"
               android:layout_gravity="center_horizontal"
               android:gravity="center_horizontal"
               android:layout_weight="1"/>
             <RadioButton android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:drawableTop="@drawable/picture2"
               android:text="动态"
               android:button="@null"
                android:id="@+id/r3"
               android:gravity="center_horizontal"
               android:layout_weight="1"/>
       </RadioGroup>
    </LinearLayout>


</LinearLayout>

如此则实现了此功能


使用java代码实现

首先要在oncreate中监听radiobutton是否checked为true

RadioGroup rg=(RadioGroup) findViewById(R.id.r);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
然后在改变图片

radiobutton = (RadioButton)findbyin(group.getchildid())//获取button
                Resources res = QQActivity.this.getResources();//使用资源
                Drawable myImage = res.getDrawable(R.drawable.home);//获取资源图片
                 button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);//四个属性分别代表drawleft drawtop.......

0 0
原创粉丝点击