资源简介
自动换行的LinearLayout,实现horizontal可以自动换行
代码片段和文件信息
package com.zcmp.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import java.util.Hashtable;
/**
* 自动换行的LinearLayout
* Created by wangxuan on 2015/11/24.
*/
public class AutoNextLineLinearlayout extends LinearLayout {
int mLeft mRight mTop mBottom;
Hashtable map = new Hashtable();
public AutoNextLineLinearlayout(Context context) {
super(context);
}
public AutoNextLineLinearlayout(Context context int horizontalSpacing int verticalSpacing) {
super(context);
}
public AutoNextLineLinearlayout(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;
mLeft = 0;
mRight = 0;
mTop = 5;
mBottom = 0;
int j = 0;
View lastview = null;
for (int i = 0; i < mCount; i++) {
final View child = getChildAt(i);
child.measure(MeasureSpec.UNSPECIFIED MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int childw = child.getMeasuredWidth();
int childh = child.getMeasuredHeight();
mX += childw; // 将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置
Position position = new Position();
mLeft = getPosition(i-j i);
mRight = mLeft + child.getMeasuredWidth();
if (mX >= mWidth) {
mX = childw;
mY += childh;
j = i;
mLeft = 0;
mRight = mLeft + child.getMeasuredWidth();
mTop = mY + 5;
// PS:如果发现高度还是有问题就得自己再细调了
}
mBottom = mTop + child.getMeasuredHeight();
mY = mTop; // 每次的高度必须记录 否则控件会叠加到一起
position.left = mLeft;
position.top = mTop + 3;
position.right = mRight;
position.bottom = mBottom;
map.put(child position);
}
setMeasuredDimension(mWidth mBottom);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(0 0); // default of 1px spacing
}
@Override
protected void onLayout(boolean changed int l int t int r int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
Position pos = (Position) map.get(child);
if (pos != null) {
child.layout(pos.left pos.top pos.right pos.bottom);
} else {
Log.i(“MyLayout“ “error“);
}
}
}
private class Position {
int left top ri
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-11-24 13:44 自动换行的LinearLayout\
文件 3496 2015-11-24 13:39 自动换行的LinearLayout\AutoNextLineLinearlayout.java
文件 128 2015-11-24 13:44 自动换行的LinearLayout\用法.txt
- 上一篇:questasim.txt
- 下一篇:VRML 大红灯笼代码
评论
共有 条评论