打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
自定义RadioButton样式2
先看一下图:


上面这种3选1的效果如何做呢?用代码写?
其实有更简单的办法,忘了RadioButton有什么特性了吗?
我就用RadioButton实现了如上效果,其实很简单的。
首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片:
Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <item   
  4.     android:state_checked="false"   
  5.     android:drawable="@drawable/tabswitcher_long" />   
  6.     <item   
  7.     android:state_checked="true"   
  8.     android:drawable="@drawable/tabswitcher_short" />   
  9. </selector>   

这里我们只要关心state_checked状态就可以了。所以很简单的配置。
接下来就是布局文件里面引用这张图片了:
Java代码  
  1. <RadioGroup  
  2.         android:gravity="center"  
  3.         android:orientation="horizontal"    
  4.         android:layout_width="fill_parent"     
  5.         android:layout_height="wrap_content"  
  6.         >  
  7.         <RadioButton     
  8.             android:id="@+id/btn_0"    
  9.             android:layout_width="fill_parent"     
  10.             android:layout_height="wrap_content"  
  11.             android:text="最新信息"   
  12.             android:textSize="17.0sp"     
  13.             android:textColor="@android:color/black"    
  14.             android:gravity="center"   
  15.             android:layout_weight="1"  
  16.             android:checked="true"  
  17.             android:button="@null"  
  18.             android:background="@drawable/radio"  
  19.             />    
  20.         <RadioButton     
  21.             android:id="@+id/btn_1"  
  22.             android:layout_width="fill_parent"     
  23.             android:layout_height="wrap_content"  
  24.             android:text="在线专家"  
  25.             android:textSize="17.0sp"  
  26.             android:textColor="@android:color/black"    
  27.             android:gravity="center"  
  28.             android:layout_weight="1"  
  29.             android:button="@null"  
  30.             android:background="@drawable/radio"  
  31.             />    
  32.         <RadioButton     
  33.             android:id="@+id/btn_2"  
  34.             android:layout_width="fill_parent"     
  35.             android:layout_height="wrap_content"  
  36.             android:text="预约服务"  
  37.             android:textSize="17.0sp"  
  38.             android:textColor="@android:color/black"    
  39.             android:gravity="center"  
  40.             android:layout_weight="1"  
  41.             android:button="@null"  
  42.             android:background="@drawable/radio"  
  43.             />    
  44.     </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  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
求教android实现短信图标右上角添加数字的问题。。跪求大神。。。
xml中,button改变背景颜色方法
Android实训案例(一)——计算器的运算逻辑
Android 自定义RadioButton 实现文字上下左右方向的图片大小设置
Android2.2——API中文文档3
5.2.2 Fragment实例精讲
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服