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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1