自定义横向的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

相关文章

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日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth