自定义控件代码:


package com.browser.testutils.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author 
 * @Description TODO 自定义组件实现换行
 * @date 2015/11/27
 */
public class DiCarQuestionTypeView extends ViewGroup {
   /** 水平方向内边距*/
    private static  int padding = 10;//水平方向padding
    /** 垂直方向内边距*/
   private static  int padding_vertical = 5;
   /** 距离父容器左右间距*/
   private static  int layout_margin = 30;
   /** 子控件间距*/
   private static  int margin = 20;

   /**
    * 
    * 构造方法,通过new关键字创建当前对象
    * 
    * @version 1.0
    * @createTime 2015-11-30,上午9:55:51
    * @updateTime 2015-11-30,上午9:55:51
    * @createAuthor 
    * @updateAuthor
    * @updateInfo (此处输入修改内容,若无修改可不写.)
    * @param context
    */
   public DiCarQuestionTypeView(Context context) {
        super(context);
    }
   
    public DiCarQuestionTypeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public DiCarQuestionTypeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();//孩子个数
        int autualWidth = r - l;//可用空间的宽度
        int x = layout_margin;// 横坐标开始
        int y = 0;//纵坐标开始
        int rows = 1;//记录行数
        for(int i = 0; i < childCount; i++){
            View view = getChildAt(i);
            //view.setBackgroundColor(Color.GREEN);
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
            x += width + margin;//计算宽度
            if(x > autualWidth){
                x = width + layout_margin + margin;//重新设置当前行的宽度开始位置
                rows++;//行数加一
            }
            y = rows*(height + margin);//计算高度
            view.layout(x - width - margin, y - height, x - margin, y);
        }
    };

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y = 0;//纵坐标
        int rows = 1;//总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int actualWidth = specWidth - layout_margin * 2;//实际宽度
        int childCount = getChildCount();
        for(int index = 0; index < childCount; index++){
            View child = getChildAt(index);
            child.setPadding(padding, padding_vertical, padding, padding_vertical);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            x += width + margin;
            if(x > actualWidth){//换行
                x = width;
                rows++;
            }
            y = rows*(height + margin);
        }
        setMeasuredDimension(actualWidth, y);
    }

    /**
     * 
     * 设置水平方向内边距
     * @version 1.0
     * @createTime 2015-11-30,上午9:52:32
     * @updateTime 2015-11-30,上午9:52:32
     * @createAuthor 
     * @updateAuthor
     * @updateInfo (此处输入修改内容,若无修改可不写.)
     * @param padding
     */
   public static void setPadding(int padding) {
      DiCarQuestionTypeView.padding = padding;
   }


   /**
    * 设置垂直方向内边距
    * (此处输入方法执行任务.)
    * 
    * @version 1.0
    * @createTime 2015-11-30,上午9:53:15
    * @updateTime 2015-11-30,上午9:53:15
    * @createAuthor 
    * @updateAuthor
    * @updateInfo (此处输入修改内容,若无修改可不写.)
    * @param padding_vertical
    */
   public static void setPadding_vertical(int padding_vertical) {
      DiCarQuestionTypeView.padding_vertical = padding_vertical;
   }


   /**
    * 设置距离父容器左右间距
    * (此处输入方法执行任务.)
    * 
    * @version 1.0
    * @createTime 2015-11-30,上午9:53:39
    * @updateTime 2015-11-30,上午9:53:39
    * @createAuthor 
    * @updateAuthor
    * @updateInfo (此处输入修改内容,若无修改可不写.)
    * @param layout_margin
    */
   public static void setLayout_margin(int layout_margin) {
      DiCarQuestionTypeView.layout_margin = layout_margin;
   }


   /**
    * 设置子控件间距
    * (此处输入方法执行任务.)
    * 
    * @version 1.0
    * @createTime 2015-11-30,上午9:54:12
    * @updateTime 2015-11-30,上午9:54:12
    * @createAuthor 
    * @updateAuthor
    * @updateInfo (此处输入修改内容,若无修改可不写.)
    * @param margin
    */
   public static void setMargin(int margin) {
      DiCarQuestionTypeView.margin = margin;
   }
}

xml中使用代码:


<com.browser.testutils.widget.DiCarQuestionTypeView
    android:id="@+id/view_wordwrap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />


main使用:

private String[] strs = new String[] { "技术类型", "技术类型技术类型", "技术类技术类型型", "技类型",
        "技术类型", "技术技术类型技术类型类型", "型", "技术型", "技术", "技术型", "技类型",
        "技术类型技术类" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //initView();
    wordWrapView = (DiCarQuestionTypeView) this.findViewById(R.id.view_wordwrap);
    for (int i = 0; i < 12; i++) {
        TextView textView = (TextView) View.inflate(this, R.layout.itme_txt_layout2, null);
        textView.setText(strs[i]);
        wordWrapView.addView(textView);
    }

}

效果图:


同时有更新:

多种标签模式效果如下: