仿书旗小说关键词UI (自定义布局)

另一篇参考http://www.eoeandroid.com/blog-529755-42702.html

仿书旗小说关键词UI , 就是按照关键词长度进行排版了.
自己实现的viewgroup

图片说明文字

 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
90
91
92
93
package com.example.testhotwordsui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
 * 热词UI 可以根据孩子的宽度自适应排版, 主要不支持任何padding
 */
public class HotWordsView extends ViewGroup{
    private int mMaxRows = 50;
    private int mWordsPadding = 10;
    private int mWordsPaddingTop = 10;
    private int mChildViewHeigh = 50; //默认高度

    public HotWordsView(Context context, AttributeSet attrs , int defStyle) {
        super(context, attrs, defStyle);
    }
    public HotWordsView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public HotWordsView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int childCount = getChildCount();

        if (childCount == 0) {
            setMeasuredDimension(0, 0);
            return;
        }
        int realRows = 1;
        int x = 0; // 计算是否超出viewgroup的宽度

        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            //热词的最大长度是 width - wordsPadding.
            view.measure(MeasureSpec.makeMeasureSpec(width - mWordsPadding, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            x += view.getMeasuredWidth() + mWordsPadding;
            if (x > width) {//如果热词排满了就添加一行, 那么现在x的坐标是width+padding
                realRows ++;
                x = view.getMeasuredWidth() + mWordsPadding;
            }
        }

        mChildViewHeigh = getChildAt(0).getMeasuredHeight();

        int realHeight = mChildViewHeigh * realRows + mWordsPaddingTop * (realRows -1);

        if (realHeight > height) {
            realHeight = height;
        }
         setMeasuredDimension(width, realHeight);
    }

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

        int childCount = getChildCount();
        int currentX = 0; //布局孩子时 , 其 右边位置

        int x = 0; //view 的左边位置
        int y = 0; //view y方向的起始位置
        int realRow = 1;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view.getVisibility() != View.GONE) {
                int viewWidth = view.getMeasuredWidth();
                x = currentX;                
                currentX += viewWidth + mWordsPadding;               
                //如果热词排满了就添加一行, 继续排版 
                if (currentX > width) {
                    currentX =  view.getMeasuredWidth() + mWordsPadding;
                    x =  0;                    
                    y = y + mChildViewHeigh + mWordsPaddingTop;
                    realRow ++;
                    if (realRow > mMaxRows) {
                        break;
                    }
                }

                view.layout( x, y, currentX - mWordsPadding, y + mChildViewHeigh );
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值