自定义自动换行组件

 

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";

private final static int VIEW_MARGIN = 2;

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) {
// Log.d(TAG, "widthMeasureSpec = " + widthMeasureSpec+
// " heightMeasureSpec" + heightMeasureSpec);
// for (int index = 0; index < getChildCount(); index++) {
// final View child = getChildAt(index);
// // measure
// child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// }
// int expandSpec = MeasureSpec.makeMeasureSpec(
// Integer.MAX_VALUE >> 1, MeasureSpec.AT_MOST);
//
//
// super.onMeasure(widthMeasureSpec, expandSpec);
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()*2;
System.out.print("height="+height);
x += width;
y = row * height + height;
if (x > maxWidth) {
x = width;
row++;
y = row * height + height;
}
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(maxWidth, y);
}

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// Log.d(TAG, "changed = " + arg0 + " left = " + arg1 + " top = " +
// arg2+ " right = " + arg3 + " botom = " + arg4);
final int count = getChildCount();
int row = 0;// which row lay you view relative to parent
int lengthX = arg1; // right position of child relative to parent
int lengthY = arg2; // bottom position of child relative to parent
for (int i = 0; i < count; i++) {

final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();

lengthX += width + VIEW_MARGIN;
lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
+ arg2;
// if it can't drawing on a same line , skip to next line
if (lengthX > arg3) {
lengthX = width + VIEW_MARGIN + arg1;
row++;
lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
+ arg2;

}

child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
}
}

}

用的时候需要动态实例化组件 

例如:在LinearLayout里添加TextView

 

<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dip"
></LinearLayout>

for (int i = 0; i < address.length; i++) {
for(String str:User_address){
if(str.equals(address[i])){
TextView tView=new TextView(mContext);
tView.setText(address[i]);
tView.setPadding(5,0,5,0);
tView.setTextColor(mContext.getResources().getColor(R.color.white));
tView.setBackgroundResource(R.color.gaem_one_color);
breakLayout.addView(tView);
LogUtils.i("line_layout"+breakLayout.getChildCount());
}
}
}

 

 

posted on 2014-04-18 09:35  楠妮儿  阅读(517)  评论(0编辑  收藏  举报

导航