Android中使用RadioButton实现QQ底部图片切换
2016-04-08 00:46
387 查看
使用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.......
然后桌布实现效果。
在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.......
相关文章推荐
- Android MVC 设计模式
- $Android自定义控件风格的方法
- GitHub Android Libraries Top100 简介
- vs2013 连接 海马玩模拟器
- Android大图片裁剪终极解决方案 原理分析
- Android,IntentService
- Android学习之Snackbar使用文档翻译
- 文件_ _android从资源文件中读取文件流并显示的方法
- android IPC机制讲解(三)
- android中关于调用键盘的问题
- 内存泄露从入门到精通三部曲之基础知识篇
- 内存泄露从入门到精通三部曲之常见原因与用户实践
- 内存泄露从入门到精通三部曲之排查方法篇
- iOS培训与Android培训的收入差距
- bug_ _图片_android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
- Android单元测试框架
- android tab底部标签栏
- Android进阶学习-使用Canvas自定义简单TextView(1)
- android自定义滑动选择开关
- 巧用setContentView的应用