Android自定义控件 - 标签流式布局

Android自定义控件 - 标签流式布局

特点:水平布局平分宽度、超出自动换行.

效果:

                     


思路:

重写onMeasure计算childView宽度,如果当前ChildView放不下当前行,那么当前行剩余的空间由当前行其它控件平分,当前childView放入下一行,如果childView大于父View宽度,那么childView.width=ViewGroup.width。(如果子控件为GONE则不测量)


重写onMeasure (代码有点粗糙^_^):

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        int currentWith = 0, visNum = 0;
        int key = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            if (childAt.getVisibility() == GONE) {
                key++;
                visNum++;
                continue;
            }
            childAt.measure(0, 0);
            int redWidth = childAt.getMeasuredWidth();
            if (redWidth + currentWith > getMeasuredWidth()) {
                int avgWidth = key < 1 ? 0 : (measuredWidth - currentWith) / (key - visNum);
                for (; key > 0; key--) {
                    View childView = getChildAt(i - key);
                    if (childView.getVisibility() == GONE)
                        continue;
                    int redWidth1 = childView.getMeasuredWidth();
                    int redHeight1 = childView.getMeasuredHeight();
                    redWidth1 += avgWidth;
                    int childMeasureWidth = MeasureSpec.makeMeasureSpec(redWidth1, MeasureSpec.EXACTLY);
                    int childMeasureHeight = MeasureSpec.makeMeasureSpec(redHeight1, MeasureSpec.EXACTLY);
                    childView.measure(childMeasureWidth, childMeasureHeight);
                }
                currentWith = 0;
                key = 0;
                visNum = 0;
            }
            key++;
            currentWith += redWidth;
        }
    }

重写onLayout:

@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int currentWidth = 0, currentHeight = 0, maxHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            if (childAt.getVisibility() == GONE)
                continue;
            int redWidth = childAt.getMeasuredWidth();
            int redHeight = childAt.getMeasuredHeight();
            maxHeight = redHeight > maxHeight ? redHeight : maxHeight;
            if (currentWidth + redWidth > r && currentWidth>0) {
                currentWidth = 0;
                currentHeight += maxHeight;
                maxHeight = 0;
            }
            redWidth = redWidth > (r - currentWidth) ? r - currentWidth : redWidth;
            setLayout(childAt, currentWidth, currentHeight, redWidth, redHeight);
            currentWidth += redWidth;




        }


    }

完整代码:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;




public class MyStreamLayout extends ViewGroup {


    public MyStreamLayout(Context context) {
        super(context);
    }


    public MyStreamLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public MyStreamLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }




    void setLayout(View v, int x, int y, int w, int h) {
        v.layout(x, y, w + x, h + y);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        int currentWith = 0, visNum = 0;
        int key = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            if (childAt.getVisibility() == GONE) {
                key++;
                visNum++;
                continue;
            }
            childAt.measure(0, 0);
            int redWidth = childAt.getMeasuredWidth();
            if (redWidth + currentWith > getMeasuredWidth()) {
                int avgWidth = key < 1 ? 0 : (measuredWidth - currentWith) / (key - visNum);
                for (; key > 0; key--) {
                    View childView = getChildAt(i - key);
                    if (childView.getVisibility() == GONE)
                        continue;
                    int redWidth1 = childView.getMeasuredWidth();
                    int redHeight1 = childView.getMeasuredHeight();
                    redWidth1 += avgWidth;
                    int childMeasureWidth = MeasureSpec.makeMeasureSpec(redWidth1, MeasureSpec.EXACTLY);
                    int childMeasureHeight = MeasureSpec.makeMeasureSpec(redHeight1, MeasureSpec.EXACTLY);
                    childView.measure(childMeasureWidth, childMeasureHeight);
                }
                currentWith = 0;
                key = 0;
                visNum = 0;
            }
            key++;
            currentWith += redWidth;
        }
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int currentWidth = 0, currentHeight = 0, maxHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            if (childAt.getVisibility() == GONE)
                continue;
            int redWidth = childAt.getMeasuredWidth();
            int redHeight = childAt.getMeasuredHeight();
            maxHeight = redHeight > maxHeight ? redHeight : maxHeight;
            if (currentWidth + redWidth > r && currentWidth > 0) {
                currentWidth = 0;
                currentHeight += maxHeight;
                maxHeight = 0;
            }
            redWidth = redWidth > (r - currentWidth) ? r - currentWidth : redWidth;
            setLayout(childAt, currentWidth, currentHeight, redWidth, redHeight);
            currentWidth += redWidth;




        }


    }




}

使用:

        <com.example.demo.MyStreamLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ChildView" />
            ......Other View
        </com.example.demo.MyStreamLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值