zoukankan      html  css  js  c++  java
  • 自定义自动换行组件

    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());
    }
    }
    }

  • 相关阅读:
    case when then用法
    查询后n条记录
    自定义函数
    字符函数
    数字运算符和函数
    时间日期函数
    mysql加密函数
    比较运算符和函数
    文件夹中的文件以目录的形式呈现
    错误提示:通过 Web 服务器的身份验证的用户无权打开文件系统上的文件
  • 原文地址:https://www.cnblogs.com/nan325/p/LineBreakLayout.html
Copyright © 2011-2022 走看看