Android自定义RadioButton及RadioGroup

本文介绍如何在Android应用中自定义RadioButton以实现TabBar,并利用Selector实现内容的居中显示,同时提供了XML布局示例和关键代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中我较习惯于使用RadioButton来实现上方或者下方的TabBar,因为RadioButton和RadioGroup有一个非常好用的check属性。

可最近的项目中使用系统自带的RadioButton时却不知道为什么发生了内容不居中的问题,又不想每次写TabBar的时候都写一大堆的布局和监听,于是就想到了自定义。

为了让自定义的view能够使用selector,我还去参照了源码的写法。

最终实现了如下效果:


xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:radioButton="http://schemas.android.com/apk/res/com.example.checkableviewtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.checkableviewtest.CustomRadioGroup 
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:orientation="horizontal"
        android:weightSum="4"
        >
	    <com.example.checkableviewtest.CustomRadioButton 
	        android:id="@+id/rb1"
	        android:layout_width="0dip"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
		    radioButton:checked="true"
		    radioButton:drawableWidth="20dip"
		    radioButton:drawableHeight="20dip"
		    radioButton:res="@drawable/selector_notice_list_item_checkbox"
		    radioButton:drawableGravity="top"
		    radioButton:text="ha1"
		    radioButton:textColor="@color/selector_main_tab"
		    radioButton:textSize="14sp"
	        >
	    </com.example.checkableviewtest.CustomRadioButton>
	    <ImageView 
	        android:layout_width="1px"
	        android:layout_height="fill_parent"
	        android:background="#ff0000"
	        />
	    <com.example.checkableviewtest.CustomRadioButton 
	        android:id="@+id/rb2"
	        android:layout_width="0dip"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
		    radioButton:drawableWidth="20dip"
		    radioButton:drawableHeight="20dip"
		    radioButton:res="@drawable/selector_notice_list_item_checkbox"
		    radioButton:drawableGravity="top"
		    radioButton:text="ha2"
		    radioButton:textColor="@color/selector_main_tab"
		    radioButton:textSize="14sp"
	        >
	    </com.example.checkableviewtest.CustomRadioButton>
	    <ImageView 
	        android:layout_width="1px"
	        android:layout_height="fill_parent"
	        android:background="#ff0000"
	        />
	    <com.example.checkableviewtest.CustomRadioButton 
	        android:id="@+id/rb3"
	        android:layout_width="0dip"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
		    radioButton:drawableWidth="20dip"
		    radioButton:drawableHeight="20dip"
		    radioButton:res="@drawable/selector_notice_list_item_checkbox"
		    radioButton:drawableGravity="top"
		    radioButton:text="ha3"
		    radioButton:textColor="@color/selector_main_tab"
		    radioButton:textSize="14sp"
	        >
	    </com.example.checkableviewtest.CustomRadioButton>
	    <ImageView 
	        android:layout_width="1px"
	        android:layout_height="fill_parent"
	        android:background="#ff0000"
	        />
	    <com.example.checkableviewtest.CustomRadioButton 
	        android:id="@+id/rb4"
	        android:layout_width="0dip"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
		    radioButton:drawableWidth="20dip"
		    radioButton:drawableHeight="20dip"
		    radioButton:res="@drawable/selector_notice_list_item_checkbox"
		    radioButton:drawableGravity="top"
		    radioButton:text="ha4"
		    radioButton:textColor="@color/selector_main_tab"
		    radioButton:textSize="14sp"
	        >
	    </com.example.checkableviewtest.CustomRadioButton>
    </com.example.checkableviewtest.CustomRadioGroup>
</RelativeLayout>

项目地址  http://download.youkuaiyun.com/detail/neverwoods/8686639

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值