android 自定义RadioButton样式
2015-06-02 21:50
609 查看
http://gundumw100.iteye.com/blog/1146527
上面这种3选1的效果如何做呢?用代码写?
其实有更简单的办法,忘了RadioButton有什么特性了吗?
我就用RadioButton实现了如上效果,其实很简单的。
首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片:
Xml代码
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/tabswitcher_long" /> <item android:state_checked="true" android:drawable="@drawable/tabswitcher_short" /> </selector>
这里我们只要关心state_checked状态就可以了。所以很简单的配置。
接下来就是布局文件里面引用这张图片了:
Java代码
<RadioGroup android:gravity="center" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/btn_0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="最新信息" android:textSize="17.0sp" android:textColor="@android :color/black" android:gravity="center" android:layout_weight="1" android:checked="true" android:button="@null" android:background="@drawable/radio" /> <RadioButton android:id="@+id/btn_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="在线专家" android:textSize="17.0sp" android:textColor="@android :color/black" android:gravity="center" android:layout_weight="1" android:button="@null" android:background="@drawable/radio" /> <RadioButton android:id="@+id/btn_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="预约服务" android:textSize="17.0sp" android:textColor="@android :color/black" android:gravity="center" android:layout_weight="1" android:button="@null" android:background="@drawable/radio" /> </RadioGroup>
注意将android:button="@null"全部设为null,并且将android:background="@drawable/radio"设为刚才新建的图片。这样就完成了。
一句代码都不用写!
看看竖下来的效果:
傍边有图标的怎么办?
也很简单啦,只要在每个RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。
另外要设置图标与文字的距离怎么办?
有一个方法setCompoundDrawablePadding(pad)可以设置图标与文字的距离,对应的属性为android:drawablePadding。
有了这招完全可以实现类似的n选1效果,看下面:
应用:
http://www.iteye.com/topic/1116261#2256664
相关文章推荐
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
- java4android(5)面向对象、类的定义、生成对象的方法、使用对象的方法、生成多个对象、构造函数super()、函数重载、static、继承
- Android手机分辨率基础知识(DPI,DIP计算)三
- Android Volley完全解析(四),带你从源码的角度理解Volley
- Android手机分辨率基础知识(DPI,DIP计算)二
- Android Volley完全解析(一),初识Volley的基本用法
- android 流量监测
- Android漂亮的对话框项目sweet-alert-dialog
- Android漂亮的对话框项目sweet-alert-dialog
- Android+Sqlite 实现古诗阅读应用(三)
- Android(java)学习笔记97:Scanner类使用
- Android与MVC设计模式
- android apk 反破解手段
- Android的ProgressBar进度条-android学习之旅(三十一)
- android 带清除功能的EditText
- Android的ProgressBar进度条-android学习之旅(三十一)
- Android的ProgressBar进度条-android学习之旅(三十一)
- Android中,控件下按效果的两种实现方式!
- 关于goole IO大会发布的android M和android studio1.3的更新
- Android底层驱动开发(一)