android 自定义View 流试布局简单例子

来源:互联网 发布:淘宝图片分辨率72 编辑:程序博客网 时间:2024/06/05 06:51
public class TextView extends ViewGroup {    private int size;    private int widthSize;    public TextView(Context context) {        super(context);    }    public TextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public TextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        measureChildren(widthMeasureSpec, heightMeasureSpec);        widthSize = MeasureSpec.getSize(widthMeasureSpec);    }    @Override    protected void onLayout(boolean c, int l, int t, int r, int b) {        int childCount = getChildCount();        int startWidth = 5;        int startheight = 0;        for (int j = 0; j < childCount; j++) {            View v = getChildAt(j);            //设置位置            v.layout(startWidth, startheight, startWidth + v.getMeasuredWidth() + 5, startheight + v.getMeasuredHeight());            startWidth += v.getMeasuredWidth();            if (startWidth >= widthSize) {                startWidth = 0;                startheight += v.getMeasuredHeight() + 10;            }        }    }}
原创粉丝点击