layout中的控件动态生成并自动换行

layout中的控件动态生成并自动换行

非常感谢原作提供的办法,找了半天才找个一个好的...


原文作者: 姩輕小妞の温柔

原文地址: http://my.eoe.cn/1185213/archive/19677.html


package com.moxiu.launcher.manager.view;

import java.util.Hashtable;

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

/**
* @author yangxiaoxue 自定义自动换行LinearLayout
*/
public class T_MyLinearLayout extends ViewGroup {

int mLeft, mRight, mTop, mBottom;

Hashtable map = new Hashtable();

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public T_MyLinearLayout(Context context) {
    super(context);
}

public T_MyLinearLayout(Context context, int horizontalSpacing,
        int verticalSpacing) {
    super(context);
}

public T_MyLinearLayout(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;
    mLeft = 0;
    mRight = 0;
    mTop = 5;
    mBottom = 0;

    int j = 0;

    View lastview = null;
    for (int i = 0; i < mCount; i++) {
        final View child = getChildAt(i);

        child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        // 此处增加onlayout中的换行判断,用于计算所需的高度
        int childw = child.getMeasuredWidth();
        int childh = child.getMeasuredHeight();
        mX += childw; // 将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置

        Position position = new Position();
        mLeft = getPosition(i - j, i);
        mRight = mLeft + child.getMeasuredWidth();
        if (mX >= mWidth) {
            mX = childw;
            mY += childh;
            j = i;
            mLeft = 0;
            mRight = mLeft + child.getMeasuredWidth();
            mTop = mY + 5;
        }
        mBottom = mTop + child.getMeasuredHeight();
        mY = mTop; // 每次的高度必须记录 否则控件会叠加到一起
        position.left = mLeft;
        position.top = mTop + 3;
        position.right = mRight;
        position.bottom = mBottom;
        map.put(child, position);
    }
    setMeasuredDimension(mWidth, mBottom);
}

@Override
protected LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(1, 1);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    int count = getChildCount();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        Position pos = (Position) map.get(child);
        if (pos != null) {
            child.layout(pos.left, pos.top, pos.right, pos.bottom);
        } else {
        }
    }
}

private class Position {
    int left, top, right, bottom;
}

public int getPosition(int IndexInRow, int childIndex) {
    if (IndexInRow > 0) {
        return getPosition(IndexInRow - 1, childIndex - 1)
                + getChildAt(childIndex - 1).getMeasuredWidth() + 8;
    }
    return getPaddingLeft();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值