Android 流水布局重写ViewGroup 代码如下
package com.test.tool;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import com.te33.yelaiban.R;/** * FlowLayout is much more like a {@link android.widget.LinearLayout}, but it can automatically * separate the widgets wrapped in it into multiple lines just like the water flow. * * Inspired by {@see http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html} * * @author liangfeizc {@see http://www.liangfeizc.com} */public class FlowLayout extends ViewGroup {    private static final int DEFAULT_HORIZONTAL_SPACING = 5;    private static final int DEFAULT_VERTICAL_SPACING = 5;    private int mVerticalSpacing;    private int mHorizontalSpacing;        public FlowLayout(Context context) {        super(context);    }    public FlowLayout(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);        try {            mHorizontalSpacing = a.getDimensionPixelSize(                    R.styleable.FlowLayout_horizontal_spacing, DEFAULT_HORIZONTAL_SPACING);            mVerticalSpacing = a.getDimensionPixelSize(                    R.styleable.FlowLayout_vertical_spacing, DEFAULT_VERTICAL_SPACING);        } finally {            a.recycle();        }    }    public void setHorizontalSpacing(int pixelSize) {        mHorizontalSpacing = pixelSize;    }    public void setVerticalSpacing(int pixelSize) {        mVerticalSpacing = pixelSize;    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int myWidth = resolveSize(0, widthMeasureSpec);        int paddingLeft = getPaddingLeft();        int paddingTop = getPaddingTop();        int paddingRight = getPaddingRight();        int paddingBottom = getPaddingBottom();        int childLeft = paddingLeft;        int childTop = paddingTop;        int lineHeight = 0;        int childHeightT = 0;        // Measure each child and put the child to the right of previous child        // if there's enough room for it, otherwise, wrap the line and put the child to next line.        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {            View child = getChildAt(i);            if (child.getVisibility() != View.GONE) {                measureChild(child, widthMeasureSpec, heightMeasureSpec);            } else {                continue;            }            int childWidth = child.getMeasuredWidth();            int childHeight = child.getMeasuredHeight();            lineHeight = Math.max(childHeight, lineHeight);            if (childLeft + childWidth + paddingRight > myWidth) {                childLeft = paddingLeft;                childTop += mVerticalSpacing + lineHeight;                lineHeight = childHeight;            } else {                childLeft += childWidth + mHorizontalSpacing;            }            childHeightT=childHeight;        }//        int wantedHeight = childTop + lineHeight + paddingBottom;        int wantedHeight = childTop + childHeightT*2 + lineHeight + paddingBottom;        setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int myWidth = r - l;        int paddingLeft = getPaddingLeft();        int paddingTop = getPaddingTop();        int paddingRight = getPaddingRight();        int childLeft = paddingLeft;        int childTop = paddingTop;        int lineHeight = 0;        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {            View childView = getChildAt(i);            if (childView.getVisibility() == View.GONE) {                continue;            }            int childWidth = childView.getMeasuredWidth();            int childHeight = childView.getMeasuredHeight();            lineHeight = Math.max(childHeight, lineHeight);            if (childLeft + childWidth + paddingRight > myWidth) {                childLeft = paddingLeft;                childTop += mVerticalSpacing + lineHeight;                lineHeight = childHeight;            }            childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);            childLeft += childWidth + mHorizontalSpacing;        }    }}
布局中的代码
   
<com.test.tool.FlowLayout    android:id="@+id/flow_layout"    flowlayout:vertical_spacing="10dp"    flowlayout:horizontal_spacing="10dp"    android:layout_gravity="center"    android:layout_marginRight="5dp"    android:layout_marginLeft="5dp"    android:layout_width="match_parent"    android:layout_height="wrap_content" >
   
<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="流水布局流水布局"/>

   
<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="流水布局流水布局"/>

   
   
<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="流水布局"/>

   
   
com.test.tool.FlowLayout>


style中的的代码
<?xml version="1.0" encoding="utf-8"?>                        
   

更多相关文章

  1. Android利用tcpdump抓包
  2. android 电话录音
  3. Android(安卓)代码监控apk安装,卸载,替换
  4. Android(安卓)studio 57 MP3 音乐播放器 代码上一曲下一曲
  5. Android(安卓)BaseAdapter 例子
  6. Android(安卓)代码监控apk安装,卸载,替换
  7. android_intent
  8. Android中九种dialog对话框代码
  9. Android语音识别

随机推荐

  1. 字体图标,布局与盒模型
  2. 酱茄OW,专为企业开发的官网小程序
  3. 测试使用的作业
  4. 注册表单实战案例
  5. 表单创建及CSS
  6. 字体图标的用法和布局的原则与元素
  7. html之盒模型,及字体图标的使用
  8. 运营级在线客服/来客客服/带非常完整的搭
  9. 图标以及盒模型
  10. 固定定位、三行三列布局