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; } } } }
android 自定义View 流试布局简单例子
最新推荐文章于 2022-07-04 17:20:30 发布