android实现左右卡片布局,Android重写ViewGroup实现卡片布局(三)

实现效果如图:

454cfe2328eb8782588d3f05d75b9ec1.png

实现思路

重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)设置每个子View的大小

重写onLayout(boolean changed, int l, int t, int r, int b) 设置每个子View的位置

第一步:新建FlowLayout继承ViewGroup

package com.rong.activity;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

import android.view.ViewGroup;

/**

* 卡片布局

*

* @author 徐荣

*

*/

public class FlowLayout extends ViewGroup {

public FlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

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

// 当前子View的数量

int childSize = getChildCount();

// 获取行宽

int lineWidth = getMeasuredWidth();

// 当前是第几行

int lines = 1;

// 当前累加的行宽

int nowLineWidth = 0;

for (int i = 0; i 

View view = getChildAt(i);

// 子View的宽度

int childWidth = view.getMeasuredWidth();

// 子View的高度

int childHeight = view.getMeasuredHeight();

// 如果当前的nowLineWidth+childWidth>= lineWidth 则换行

if (nowLineWidth + childWidth >= lineWidth) {

nowLineWidth = 0;

lines = lines + 1;

}

// 设置子View的位置

view.layout(nowLineWidth, childHeight * (lines - 1), nowLineWidth + childWidth, childHeight * lines);

nowLineWidth = nowLineWidth + childWidth;

// 如果nowLineWidth >= lineWidth 则换行

if (nowLineWidth >= lineWidth) {

nowLineWidth = 0;

lines = lines + 1;

}

}

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// 设置自己View的大小

setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);

for (int i = 0; i 

View view = getChildAt(i);

// 设置每个子View的大小

view.measure(view.getMeasuredWidth(), view.getMeasuredHeight());

}

}

}

第二步:新建布局文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/black"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Apple" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Cup" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Double" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Ear" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Flower" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Game" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hotdog" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="interseting" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="joker" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="king" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="mother" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="lost" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="noting" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="orange" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="poker" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="qustion" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="ring" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="string" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="type" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="unit" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="vertion" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="west" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="x" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="young" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="zip" />

Run

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值