吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3740|回复: 3
收起左侧

[其他转载] 全对齐布局(上下左右都对齐) (基于流式布局)demo

[复制链接]
djwdj 发表于 2018-1-31 11:20


先上核心代码:
[Java] 纯文本查看 复制代码
class ll extends ViewGroup
{
	int n=2,H,V,max;

	ll(Context c){super(c);}

	void set(int n, int h, int v)
	{
		this.n = n;
		this.H = h;
		this.V = v;
	}
	
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
		max = maxWidth - getPaddingLeft() - getPaddingRight();
        int x = 0;
        int y = getPaddingTop() + getPaddingBottom();
        int row = 0;
        for (int i = 0; i < getChildCount(); i++)
		{
			View v = getChildAt(i);
            if (v.getVisibility() != View.GONE)
			{
                v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                int height = v.getMeasuredHeight();
                x += 1;
                y = row * height + height;
                if (x > n)
				{
                    x = 1;
                    row++;
                    y = row * height + height;
                }
            }
        }
		y += (row + 2) * V;
        setMeasuredDimension(maxWidth, y);
    }

	@Override
	protected void onLayout(boolean p1, int l, int t, int r, int b)
	{
		l = getPaddingLeft();
		t = getPaddingTop();
		int x = l;
        int y = 0;
        int row = 1;
        for (int i = 0; i < getChildCount(); i++)
		{
			View v = getChildAt(i);
            if (v.getVisibility() != View.GONE)
			{
                int width = (max - H) / n - H;
                int height = v.getMeasuredHeight();
                x += width + H;
                y = row * (height + V);
                if (x > l + max)
				{
                    x = l + H + width;
                    row++;
                    y = row * (height + V);
                }
				v.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
						  MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                v.layout(x - width, y - height, x, y);
            }
        }
	}
}

核心代码基于尘哥bamboy
再上调用的
[Java] 纯文本查看 复制代码
		ll f=new ll(this);
		f.setPadding(28,8,28,8);
		f.set(3,32,32);//一行的排布数,横间距,纵间距

左右上下都对齐

左右上下都对齐

demo.zip

13.7 KB, 下载次数: 20, 下载积分: 吾爱币 -1 CB

全对齐布局

免费评分

参与人数 3吾爱币 +3 热心值 +3 收起 理由
jianailing + 1 + 1 热心回复!
cn52pojie + 1 热心回复!
孟坤软件 + 2 + 1 学习了

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
ufo8636278 发表于 2018-1-31 11:37
提示: 作者被禁止或删除 内容自动屏蔽
Moira-Luke 发表于 2018-1-31 11:42
huste 发表于 2018-2-1 13:25
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 12:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表