自定义横向的RadioGroup:HorizontalRadioGroup
来源:互联网 发布:linux重启命令init 编辑:程序博客网 时间:2024/05/17 07:35
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);}@Overrideprotected 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;}@Overrideprotected 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; }}
}
阅读全文
0 0
- 自定义横向的RadioGroup:HorizontalRadioGroup
- 自定义多行多列的RadioGroup
- 自定义横向的ListView
- 自定义横向的ScrollView
- Android RadioGroup 横向显示
- 支持自定义布局的RadioGroup
- Android RadioGroup中横向、竖向布局RadioButton的问题
- Android RadioGroup中横向、竖向布局RadioButton的问题
- 自定义的radioGroup和自定义的editText
- 自定义:RadioGroup
- Android自定义横向的ProgressBar
- Android自定义横向的ProgressBar
- RadioGroup以及RadioButton自定义样式的使用
- 自定义支持多行显示的RadioGroup
- .NET 横向打印,自定义纸张的横向打印
- Android 自定义横向的ProgressBar的颜色
- 自定义控件HorizontalListView,横向的ListView
- 自定义控件HorizontalListView,横向的ListView
- vim插件之quickfix
- MVC在b/s系统下的应用图解
- centos 7.2关闭防火墙
- 安装node
- java 根据开始和结束日期得到之间所有日期集合
- 自定义横向的RadioGroup:HorizontalRadioGroup
- Matlab修改数值格式/精度/小数位数
- Handler机制
- 计算机原理:第二,gcc使用大全
- 数组和链表的区别
- 类微博系统
- java 数组length和字符串length的区别
- li标签实现不同行颜色不一样(奇数行、偶数行)
- 字符串模拟乘法