吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2829|回复: 2
收起左侧

[其他转载] 基式布局 demo 1.0

[复制链接]
djwdj 发表于 2018-2-10 22:35
基式布局兼容性强,还支持安卓2.3,流畅度高。可完美取代GridView。
只需轻松配置好,就可以不断向基式布局加子布局了
先上核心代码:
[Java] 纯文本查看 复制代码
class ll extends ViewGroup
{
	int n=2,H,V,W;

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

	public
	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);
		W = maxWidth - getPaddingLeft() - getPaddingRight();
        int x = 0;
        int y = 0;
        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+getPaddingTop() + getPaddingBottom();
        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 = (W - H) / n - H;
                int height = v.getMeasuredHeight();
                x += width + H;
                y = row * (height + V)+t;
                if (x > l + W)
				{
                    x = l + H + width;
                    row++;
                    y = row * (height + V)+t;
                }
				v.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
						  MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                v.layout(x - width, y - height, x, y);
            }
        }
	}
}

.set(一行的子布局数目,横向间距,纵向间距)
基式布局已完美适配内间距了,可直接setPadding

demo:
[Java] 纯文本查看 复制代码
public class l extends Activity implements OnClickListener
{
	String[] ss = 
	{
		"QQ",
		"视频",
		"放开那三国",
		"电子书",
		"酒店",
		"单机",
		"小说",
		"斗地主",
		"优酷",
		"网游",
		"WIFI万能钥匙",
		"播放器",
		"捕鱼达人2",
		"机票",
		"游戏",
		"熊出没之熊大快跑",
		"美图秀秀",
		"浏览器",
		"单机游戏",
		"我的世界",
		"电影电视",
		"QQ空间",
		"旅游",
		"免费游戏",
		"2048",
		"刀塔传奇",
		"壁纸",
		"节奏大师",
		"锁屏",
		"装机必备",
		"天天动听",
		"备份",
		"网盘",
		"海淘网",
		"大众点评",
		"爱奇艺视频",
		"腾讯手机管家",
		"百度地图",
		"猎豹清理大师",
		"谷歌地图",
		"hao123上网导航",
		"京东",
		"有你",
		"万年历-农历黄历",
		"支付宝钱包"
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		
		ll l=new ll(this);
		l.setPadding(28,38,28,58);
		l.set(3,32,32);
		
        Random random = new Random();
        // 循环添加TextView到容器
        for (int i = 0; i < ss.length; i++) {
            TextView t = new Button(this);
            t.setText(ss[i]);
            t.setTextColor(Color.WHITE);
            t.setPadding(5, 5, 5, 5);
            t.setGravity(Gravity.CENTER);
            t.setTextSize(14);
            t.setOnClickListener(this);
            // 设置彩色背景
            GradientDrawable normalDrawable = new GradientDrawable();
            normalDrawable.setShape(GradientDrawable.RECTANGLE);
            int a = 255;
            int r = 50 + random.nextInt(150);
            int g = 50 + random.nextInt(150);
            int b = 50 + random.nextInt(150);
            normalDrawable.setColor(Color.argb(a, r, g, b));
            // 设置按下的灰色背景
            GradientDrawable pressedDrawable = new GradientDrawable();
            pressedDrawable.setShape(GradientDrawable.RECTANGLE);
            pressedDrawable.setColor(Color.GRAY);
            // 背景选择器
            StateListDrawable stateDrawable = new StateListDrawable();
            stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
            stateDrawable.addState(new int[]{}, normalDrawable);
            // 设置背景选择器到TextView上
            t.setBackgroundDrawable(stateDrawable);
            l.addView(t);
        }
		ScrollView s=new ScrollView(this);
		s.setBackgroundColor(0xff333333);
		s.addView(l);
		addContentView(s,new WindowManager.LayoutParams());
    }

	@Override
	public void onClick(View v) 
	{
		Toast.makeText(this, ((TextView)v).getText(), Toast.LENGTH_SHORT).show();
	}
}

Screenshot_20180210-223400.jpg
Screenshot_20180210-223343.jpg
Screenshot_20180210-223331.jpg

基式布局demo 1.0.zip

27.92 KB, 下载次数: 7, 下载积分: 吾爱币 -1 CB

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

psx1lin 发表于 2018-2-10 23:01
值的研究
感謝..
狼来了呀 发表于 2018-2-11 07:36 来自手机
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-15 13:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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