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

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



然后桌布实现效果。

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



   

 

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


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/co">
                    android:layout_height="match_parent"
            android:id="@+id/col"
            android:background="#000"/>
   

            android:layout_height="1dp"
        android:background="#ccc"/>
            android:layout_height="wrap_content"
        android:orientation="horizontal">
                   android:layout_height="wrap_content"
            android:id="@+id/r"
           android:orientation="horizontal">
                           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"/>
                           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"/>
                             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"/>
       

   




如此则实现了此功能


使用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.......

你可能感兴趣的:(Android中使用RadioButton实现QQ底部图片切换)