自定义RadioButton样式2

先看一下图:

[img]http://dl.iteye.com/upload/attachment/534640/4b62ad6c-cc7b-3a84-9c18-175b53846b4d.png[/img]
上面这种3选1的效果如何做呢?用代码写?
其实有更简单的办法,忘了RadioButton有什么特性了吗?
我就用RadioButton实现了如上效果,其实很简单的。
首先定义一张background,命名为radio.xml,注意该background必须为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状态就可以了。所以很简单的配置。
接下来就是布局文件里面引用这张图片了:

<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"设为刚才新建的图片。这样就完成了。
一句代码都不用写!

看看竖下来的效果:
[img]http://dl.iteye.com/upload/attachment/534654/6c01dd53-f522-3cc5-8ab2-30f2b2004fe8.png[/img]

傍边有图标的怎么办?

[img]http://dl.iteye.com/upload/attachment/534656/6a547f97-3de6-38f4-9e32-e230a1002688.png[/img]
也很简单啦,只要在每个RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。
另外要设置图标与文字的距离怎么办?
有一个方法setCompoundDrawablePadding(pad)可以设置图标与文字的距离,对应的属性为android:drawablePadding。

有了这招完全可以实现类似的n选1效果,看下面:
[img]http://dl.iteye.com/upload/attachment/534659/44f72dc1-654c-37bc-9816-9d2d8c4b628e.png[/img]
[img]http://dl.iteye.com/upload/attachment/534661/bc064bd6-4b0c-33d6-9331-47b5ad6d8c8c.png[/img]

应用:
[url]http://www.iteye.com/topic/1116261#2256664[/url]

CircleCheckBox
[url]https://github.com/CoXier/CheckBox[/url]
SwitchButton
[url]https://github.com/kyleduo/SwitchButton[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值