andorid 流式标签布局

来源:互联网 发布:c语言文件读写 编辑:程序博客网 时间:2024/05/16 12:17

android 动态布局标签每行标签居中显示

public class TagGroupView extends ViewGroup {
private int LINE_SPACE = 15;
private int COLUMN_SPACE = 10;

private int mLineSpace;private int mColumnSpace;private ArrayList<View> views = new ArrayList<>();private int gravity = Gravity.CENTER_HORIZONTAL;public TagGroupView(Context context) {    this(context, null);}public TagGroupView(Context context, AttributeSet attrs) {    super(context, attrs);    final TypedArray a = context.obtainStyledAttributes(            attrs, ATTRS);    mCheckedPosition = a.getInt(0, -1);    a.recycle();}@TargetApi(Build.VERSION_CODES.HONEYCOMB)public TagGroupView(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    final TypedArray a = context.obtainStyledAttributes(            attrs, ATTRS, defStyleAttr, defStyleAttr);    mCheckedPosition = a.getInt(0, -1);    a.recycle();}{    mLineSpace = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, LINE_SPACE,            getResources().getDisplayMetrics());    mColumnSpace = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, COLUMN_SPACE,            getResources().getDisplayMetrics());}@Overrideprotected ViewGroup.LayoutParams generateDefaultLayoutParams() {    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);}@Overrideprotected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {    return new LayoutParams(p);}@Overridepublic ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {    return new LayoutParams(getContext(), attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int childCount = getChildCount();    if(childCount == 0){        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    int width = MeasureSpec.getSize(widthMeasureSpec);    int contentWidth = width - getPaddingLeft() - getPaddingRight();    int lineWidth = 0;    int lines = 0;    int lineHeight = 0;    int column = 0;    for (int i = 0; i < childCount; i++) {        View childAt = getChildAt(i);        if(childAt.getVisibility() == GONE)continue;        LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();        measureChild(childAt, widthMeasureSpec, heightMeasureSpec);        lineWidth += childAt.getMeasuredWidth();        lineHeight = childAt.getMeasuredHeight();        if(lineWidth > contentWidth) {            lines++;            lineWidth = childAt.getMeasuredWidth();            column = 0;        } else {            lineWidth += mColumnSpace;            column ++;        }        layoutParams.columnInLine = column;        layoutParams.lineIndex = lines;    }    int height = getPaddingTop() + getPaddingBottom() + (lines + 1) * lineHeight + mLineSpace * lines;    setMeasuredDimension(width, height);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {    if(gravity == Gravity.LEFT) {        onLayoutLeftAlign();    } else if(gravity == Gravity.CENTER_HORIZONTAL) {        onLayoutCenterAlign();    }}private void onLayoutCenterAlign() {    int childCount = getChildCount();    int lineWidth = 0;    for (int i = 0; i < childCount; i++) {        View childAt = getChildAt(i);        if(childAt.getVisibility() == GONE){            continue;        }        LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();        views.add(childAt);        lineWidth += childAt.getMeasuredWidth();        if(i+ 1 < childCount){            View nextChild = getChildAt(i + 1);            if(nextChild.getVisibility() == GONE)continue;            LayoutParams lp = (LayoutParams) nextChild.getLayoutParams();            if(layoutParams.lineIndex < lp.lineIndex){                layoutLine(lineWidth, childAt.getMeasuredHeight(), layoutParams);                lineWidth = 0;            }        } else {            layoutLine(lineWidth, childAt.getMeasuredHeight(), layoutParams);            lineWidth = 0;        }    }}private void layoutLine(int lineWidth, int itemHeight,  LayoutParams layoutParams) {    int contentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();    int left = getPaddingLeft() +(contentWidth - lineWidth- (views.size() - 1) * mColumnSpace) / 2;    int right = 0;    int top = getPaddingTop() + (layoutParams.lineIndex * (mLineSpace + itemHeight));    for (int j = 0; j < views.size(); j++) {        View view = views.get(j);        right = left+ view.getMeasuredWidth();        view.layout(left, top, right, top + view.getMeasuredHeight());        left  = right + mColumnSpace;    }    views.clear();}private void onLayoutLeftAlign() {    int childCount = getChildCount();    int lineLeft = 0;    for (int i = 0; i < childCount; i++) {        View childAt = getChildAt(i);        if(childAt.getVisibility() == GONE){            continue;        }        LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();        if(layoutParams.columnInLine == 0) {            lineLeft = getPaddingLeft();        }        int left = lineLeft;        lineLeft += childAt.getMeasuredWidth();        int top = (layoutParams.lineIndex * (mLineSpace + childAt.getMeasuredHeight()));        childAt.layout(left, top, lineLeft, top + childAt.getMeasuredHeight());        lineLeft += mLineSpace;    }}class LayoutParams extends ViewGroup.LayoutParams{    int lineIndex = 0;    int columnInLine = 0;    public LayoutParams(Context c, AttributeSet attrs) {        super(c, attrs);    }    public LayoutParams(int width, int height) {        super(width, height);    }    public LayoutParams(ViewGroup.LayoutParams source) {        super(source);    }}

}
“`

0 0
原创粉丝点击