您的位置:首页 > 移动开发 > Android开发

android定义组件自动换行

2013-02-22 17:27 225 查看
让容器中的组件自动换行,之前看过一个例子,但没有根据容器计算高度。

在此基础上修改了一点代码,以适应自动的高度计算,自己试了下,貌似还可以。

package com.example.exam;

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

public class LineBreakLayout extends ViewGroup
{
private final static String TAG = "LineBreakLayout";

public LineBreakLayout(Context context)
{
super(context);
}

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

public LineBreakLayout(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;
int mRow = 0;

for (int index = 0; index < mCount; index++)
{
final View child = getChildAt(index);
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
mX += width;
mY = mRow * height + height;
if (mX > mWidth)
{
mX = width;
mRow++;
mY = mRow * height + height;
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(mWidth, mY);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
final int count = getChildCount();
t = 0;
int mX = l;
int mY = t;
int mRow = 0;
for (int i = 0; i < count; i++)
{
final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
mX += width;
mY = mRow * height + height + t;
if (mX > r)
{
mX = width + l;
mRow++;
mY = mRow * height + height + t;
}
child.layout(mX - width, mY - height, mX, mY);
}
}
}


修正(去除左边和顶部的间隙,添加是否隐藏的判断):

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
int childCount = getChildCount();
int x = 0;
int y = 0;
int row = 0;

for (int index = 0; index < childCount; index++) {
final View child = getChildAt(index);
if (child.getVisibility() != View.GONE) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
if (x > maxWidth) {
x = width;
row++;
y = row * height + height;
}
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(maxWidth, y);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
int maxWidth = r - l;
int x = 0;
int y = 0;
int row = 0;
for (int i = 0; i < childCount; i++) {
final View child = this.getChildAt(i);
if (child.getVisibility() != View.GONE) {
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
if (x > maxWidth) {
x = width;
row++;
y = row * height + height;
}
child.layout(x - width, y - height, x, y);
}
}
}


当一行的第一个子元素超过宽度时,不进行换行处理

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
int childCount = getChildCount();
int x = 0;
int y = 0;
int row = 0;

int count = 0;
for (int index = 0; index < childCount; index++) {
final View child = getChildAt(index);
if (child.getVisibility() != View.GONE) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
count++;
if (x > maxWidth) {
x = width;
if (count > 1) {
y = ++row * height + height;
} else {
y = row * height + height;
count = 1;
}
}
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(maxWidth, y + row * BOTTOM_MARGIN + 10);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
int maxWidth = r - l;
int x = 0;
int y = 0;
int row = 0;
int count = 0;
for (int i = 0; i < childCount; i++) {
final View child = this.getChildAt(i);
if (child.getVisibility() != View.GONE) {
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
count++;
if (x > maxWidth) {
x = width;
if (count > 1) {
y = ++row * height + height;
} else {
y = row * height + height;
count = 1;
}
}
child.layout(x - width, row * BOTTOM_MARGIN + y - height, x,
row * BOTTOM_MARGIN + y);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: