首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

自定义ViewGroup兑现自动换行的布局

2012-08-10 
自定义ViewGroup实现自动换行的布局  viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一

自定义ViewGroup实现自动换行的布局
  viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一个可以自动根据一行中view的宽度自动换行的布局,网上找了下,没有相关的例子,但是找到了思路:自定义一个viewgroup,然后在onlayout文件里面自动检测view的右边缘的横坐标值,和你的view的parent view的况度判断是否换行显示view就可以了。因为代码比较简单,就不多说了:



http://www.cnblogs.com/slider/archive/2011/11/24/2262161.html

还可以试试这个:
import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * * @author RAW */public class FlowLayout extends ViewGroup {    private final static int PAD_H = 2, PAD_V = 2; // Space between child views.    private int mHeight;    public FlowLayout(Context context) {        super(context);    }    public FlowLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);        final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();        final int count = getChildCount();        int xpos = getPaddingLeft();        int ypos = getPaddingTop();        int childHeightMeasureSpec;        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);        else            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);        mHeight = 0;        for(int i = 0; i < count; i++) {            final View child = getChildAt(i);            if(child.getVisibility() != GONE) {                child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);                final int childw = child.getMeasuredWidth();                mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);                if(xpos + childw > width) {                    xpos = getPaddingLeft();                    ypos += mHeight;                }                xpos += childw + PAD_H;            }        }        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {            height = ypos + mHeight;        } else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {            if(ypos + mHeight < height) {                height = ypos + mHeight;            }        }        height += 5; // Fudge to avoid clipping bottom of last row.        setMeasuredDimension(width, height);    } // end onMeasure()    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        final int width = r - l;        int xpos = getPaddingLeft();        int ypos = getPaddingTop();        for(int i = 0; i < getChildCount(); i++) {            final View child = getChildAt(i);            if(child.getVisibility() != GONE) {                final int childw = child.getMeasuredWidth();                final int childh = child.getMeasuredHeight();                if(xpos + childw > width) {                    xpos = getPaddingLeft();                    ypos += mHeight;                }                child.layout(xpos, ypos, xpos + childw, ypos + childh);                xpos += childw + PAD_H;            }        }    } // end onLayout()}


How to Implement Flipboard Animation on Android

热点排行