android自定义RadioGroup实现可以添加多种布局

2024-02-26 06:32

本文主要是介绍android自定义RadioGroup实现可以添加多种布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自

 

android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到radiobutton,如果要实现这种效果呢,于是看了RadioGroup的源码,发现问题在于addView方法和自定义的PassThroughHierarchyChangeListener;

下面就这两个地方动手脚,先拷贝源码,然后去掉RadioGroup(Context context, AttributeSet attrs) 中的方法,我新增了一个方法,查找viewGroup控件中的radioButton,

复制代码
/** 查找radioButton控件 */public RadioButton findRadioButton(ViewGroup group) {RadioButton resBtn = null;int len = group.getChildCount();for (int i = 0; i < len; i++) {if (group.getChildAt(i) instanceof RadioButton) {resBtn = (RadioButton) group.getChildAt(i);} else if (group.getChildAt(i) instanceof ViewGroup) {findRadioButton((ViewGroup) group.getChildAt(i));}}return resBtn;}
复制代码

addView的实现如下:

复制代码
 1 @Override
 2     public void addView(View child, int index, ViewGroup.LayoutParams params) {
 3         if (child instanceof RadioButton) {
 4             final RadioButton button = (RadioButton) child;
 5             if (button.isChecked()) {
 6                 mProtectFromCheckedChange = true;
 7                 if (mCheckedId != -1) {
 8                     setCheckedStateForView(mCheckedId, false);
 9                 }
10                 mProtectFromCheckedChange = false;
11                 setCheckedId(button.getId());
12             }
13         } else if (child instanceof ViewGroup) {  //这里是我添加的代码
14             final RadioButton button = findRadioButton((ViewGroup) child);
15             if (button.isChecked()) {
16                 mProtectFromCheckedChange = true;
17                 if (mCheckedId != -1) {
18                     setCheckedStateForView(mCheckedId, false);
19                 }
20                 mProtectFromCheckedChange = false;
21                 setCheckedId(button.getId());
22             }
23         }
24 
25         super.addView(child, index, params);
26     }
复制代码

然后在自定义的PassThroughHierarchyChangeListener中修改:

复制代码
private class PassThroughHierarchyChangeListener implementsViewGroup.OnHierarchyChangeListener {private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;public void onChildViewAdded(View parent, View child) {if (parent == MyRadioGroup.this && child instanceof RadioButton) {int id = child.getId();// generates an id if it's missingif (id == View.NO_ID) {id = child.hashCode();child.setId(id);}((RadioButton) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);} else if (parent == MyRadioGroup.this   //这里是我添加的代码&& child instanceof ViewGroup) {RadioButton btn = findRadioButton((ViewGroup) child);int id = btn.getId();// generates an id if it's missingif (id == View.NO_ID) {id = btn.hashCode();btn.setId(id);}btn.setOnCheckedChangeListener(mChildOnCheckedChangeListener);}if (mOnHierarchyChangeListener != null) {mOnHierarchyChangeListener.onChildViewAdded(parent, child);}}public void onChildViewRemoved(View parent, View child) {if (parent == MyRadioGroup.this && child instanceof RadioButton) {((RadioButton) child).setOnCheckedChangeListener(null);} else if (parent == MyRadioGroup.this&& child instanceof ViewGroup) {findRadioButton((ViewGroup) child).setOnCheckedChangeListener(null);}if (mOnHierarchyChangeListener != null) {mOnHierarchyChangeListener.onChildViewRemoved(parent, child);}}}
复制代码

做好了上面的步骤,下面布局就可以按照自己的意思来了,并且分组效果也不会影响

下面我的布局文件demo:

复制代码
 <com.huaheng.wy.view.MyRadioGroupandroid:id="@+id/rg"android:layout_width="fill_parent"android:layout_height="65dp"android:layout_alignParentBottom="true"android:background="@drawable/yst_i_bottom"android:orientation="horizontal" ><!-- 首页 --><FrameLayoutandroid:id="@+id/left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1" ><RadioButtonandroid:id="@+id/rbtn_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomhome_selector"android:gravity="center_horizontal"android:text="首页"android:textColor="@color/white"android:textSize="@dimen/font_5" /><Buttonandroid:id="@+id/btn_home_point"android:layout_width="21dp"android:layout_height="20dp"android:layout_gravity="top|right"android:layout_marginRight="10dp"android:layout_marginTop="0dp"android:background="@drawable/notice_bg"android:text="1"android:textColor="@color/white"android:textSize="@dimen/font_6"android:visibility="gone" /></FrameLayout><!-- 历史 --><RadioButtonandroid:id="@+id/rbtn_his"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomhis_selector"android:gravity="center_horizontal"android:text="历史"android:textColor="@color/white"android:textSize="@dimen/font_5" /><!-- 扫描 --><Buttonandroid:id="@+id/rbtn_scan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:background="@drawable/scan_selector"android:button="@null"android:gravity="center_horizontal|bottom"android:paddingBottom="7dp"android:text="扫描"android:textColor="@color/white"android:textSize="@dimen/font_5" /><RadioButtonandroid:id="@+id/rbtn_seek"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottomseek_selector"android:gravity="center_horizontal"android:text="搜索"android:textColor="@color/white"android:textSize="@dimen/font_5" /><RadioButtonandroid:id="@+id/rbtn_more"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginBottom="2dp"android:layout_marginTop="3dp"android:layout_weight="1"android:background="@null"android:button="@null"android:drawableTop="@drawable/yst_i_bottommore_selector"android:gravity="center_horizontal"android:text="更多"android:textColor="@color/white"android:textSize="@dimen/font_5" /></com.huaheng.wy.view.MyRadioGroup>

这篇关于android自定义RadioGroup实现可以添加多种布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/747943

相关文章

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方