android定义组件自动换行

让容器中的组件自动换行,之前看过一个例子,但没有根据容器计算高度。

在此基础上修改了一点代码,以适应自动的高度计算,自己试了下,貌似还可以。


package com.example.exam;

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

public class LineBreakLayout extends ViewGroup
{
    private final static String TAG = "LineBreakLayout";
    
    public LineBreakLayout(Context context)
    {
        super(context);
    }
    
    public LineBreakLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    public LineBreakLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int mWidth = MeasureSpec.getSize(widthMeasureSpec);
        int mCount = getChildCount();
        int mX = 0;
        int mY = 0;
        int mRow = 0;
        
        for (int index = 0; index < mCount; index++)
        {
            final View child = getChildAt(index);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            // 此处增加onlayout中的换行判断,用于计算所需的高度
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            mX += width;
            mY = mRow * height + height;
            if (mX > mWidth)
            {
                mX = width;
                mRow++;
                mY = mRow * height + height;
            }
        }
        // 设置容器所需的宽度和高度
        setMeasuredDimension(mWidth, mY);
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        final int count = getChildCount();
        t = 0;
        int mX = l;
        int mY = t;
        int mRow = 0;
        for (int i = 0; i < count; i++)
        {
            final View child = this.getChildAt(i);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            mX += width;
            mY = mRow * height + height + t;
            if (mX > r)
            {
                mX = width + l;
                mRow++;
                mY = mRow * height + height + t;
            }
            child.layout(mX - width, mY - height, mX, mY);
        }
    }
}

修正(去除左边和顶部的间隙,添加是否隐藏的判断):

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childCount = getChildCount();
		int x = 0;
		int y = 0;
		int row = 0;

		for (int index = 0; index < childCount; index++) {
			final View child = getChildAt(index);
			if (child.getVisibility() != View.GONE) {
				child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
				// 此处增加onlayout中的换行判断,用于计算所需的高度
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				if (x > maxWidth) {
					x = width;
					row++;
					y = row * height + height;
				}
			}
		}
		// 设置容器所需的宽度和高度
		setMeasuredDimension(maxWidth, y);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int childCount = getChildCount();
		int maxWidth = r - l;
		int x = 0;
		int y = 0;
		int row = 0;
		for (int i = 0; i < childCount; i++) {
			final View child = this.getChildAt(i);
			if (child.getVisibility() != View.GONE) {
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				if (x > maxWidth) {
					x = width;
					row++;
					y = row * height + height;
				}
				child.layout(x - width, y - height, x, y);
			}
		}
	}


当一行的第一个子元素超过宽度时,不进行换行处理

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childCount = getChildCount();
		int x = 0;
		int y = 0;
		int row = 0;

		int count = 0;
		for (int index = 0; index < childCount; index++) {
			final View child = getChildAt(index);
			if (child.getVisibility() != View.GONE) {
				child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
				// 此处增加onlayout中的换行判断,用于计算所需的高度
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				count++;
				if (x > maxWidth) {
					x = width;
					if (count > 1) {
						y = ++row * height + height;
					} else {
						y = row * height + height;
						count = 1;
					}
				}
			}
		}
		// 设置容器所需的宽度和高度
		setMeasuredDimension(maxWidth, y + row * BOTTOM_MARGIN + 10);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int childCount = getChildCount();
		int maxWidth = r - l;
		int x = 0;
		int y = 0;
		int row = 0;
		int count = 0;
		for (int i = 0; i < childCount; i++) {
			final View child = this.getChildAt(i);
			if (child.getVisibility() != View.GONE) {
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				count++;
				if (x > maxWidth) {
					x = width;
					if (count > 1) {
						y = ++row * height + height;
					} else {
						y = row * height + height;
						count = 1;
					}
				}
				child.layout(x - width, row * BOTTOM_MARGIN + y - height, x,
						row * BOTTOM_MARGIN + y);
			}
		}
	}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值