自定义横向的RadioGroup:HorizontalRadioGroup

2024-05-11 10:38

本文主要是介绍自定义横向的RadioGroup:HorizontalRadioGroup,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

package com.li.newhuangjinbo.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

/**

  • 自定义横向radioGroup
    */

public class HorizontalRadioGroup extends RadioGroup {
public HorizontalRadioGroup(Context context) {
this(context,null);
}

public HorizontalRadioGroup(Context context, AttributeSet attrs) {super(context, attrs);setOrientation(LinearLayout.HORIZONTAL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width=measureWidth(widthMeasureSpec);int height=measureHeight(heightMeasureSpec);setMeasuredDimension(width,height);
}private int measureHeight(int heightMeasureSpec) {int size= MeasureSpec.getSize(heightMeasureSpec);int mode= MeasureSpec.getMode(heightMeasureSpec);// 先测量所有的子View// 在测量自己int result=0;measureAllChildHeight(mode,size-getPaddingBottom()-getPaddingTop());if(mode== MeasureSpec.EXACTLY){result=size;}else{result=getChildAt(0).getMeasuredHeight();if(mode== MeasureSpec.AT_MOST){result= Math.min(result,size);}}return size;
}private void measureAllChildHeight(int mode, int width) {int childCount = getChildCount();for(int i=0;i<childCount;i++) {View childAt = getChildAt(i);LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) childAt.getLayoutParams();int childHeight = params.height;switch (mode) {case MeasureSpec.EXACTLY:if(childHeight>=0){childAt.measure(0, MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));}else if(childHeight== LinearLayout.LayoutParams.MATCH_PARENT) {childAt.measure(0, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY));}else{childAt.measure(0, MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST));}break;case MeasureSpec.AT_MOST:if(childHeight>=0){childAt.measure(0, MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));}else if(childHeight== LinearLayout.LayoutParams.MATCH_PARENT) {childAt.measure(0, MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST));}else{childAt.measure(0, MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST));}break;}}
}private int measureWidth(int widthMeasureSpec) {int size= MeasureSpec.getSize(widthMeasureSpec);int mode= MeasureSpec.getMode(widthMeasureSpec);// 先测量所有的子View// 在测量自己int result=0;measureAllChildWidth(mode,size-getPaddingRight()-getPaddingLeft());if(mode== MeasureSpec.EXACTLY){result=size;}else{result=allChildWidth();if(mode== MeasureSpec.AT_MOST){result= Math.min(result,size);}}return size;
}private void measureAllChildWidth(int mode, int width) {int childCount = getChildCount();for(int i=0;i<childCount;i++) {View childAt = getChildAt(i);LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) childAt.getLayoutParams();int childHeight = params.width;switch (mode) {case MeasureSpec.EXACTLY:if(childHeight>=0){childAt.measure(MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY),0);}else if(childHeight== LinearLayout.LayoutParams.MATCH_PARENT) {childAt.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),0);}else{childAt.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),0);}break;case MeasureSpec.AT_MOST:if(childHeight>=0){childAt.measure(0, MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));}else if(childHeight== LinearLayout.LayoutParams.MATCH_PARENT) {childAt.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),0);}else{childAt.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),0);}break;}}
}private int allChildWidth() {int size=0;for(int i=0;i<getChildCount();i++){size+=getChildAt(i).getMeasuredWidth();}return size;
}@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {int childCount=getChildCount();int allWidth = getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-childCount*getChildAt(0).getMeasuredWidth();int path=allWidth/(childCount-1);int left=getPaddingLeft();for(int i=0;i<childCount;i++){View child = getChildAt(i);child.layout(left,0,left+child.getMeasuredWidth(),child.getMeasuredHeight());left=child.getRight()+path;}
}

}

这篇关于自定义横向的RadioGroup:HorizontalRadioGroup的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各