本文主要是介绍自定义横向的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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!