您的位置:首页 > 移动开发 > Android开发

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

2017-11-05 19:20 447 查看
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;
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: