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

}

原创粉丝点击