新建类
继承viewgroup 实现3个构造方法
private int w1=20;
private int t1=20;
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return super.generateLayoutParams(p);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//方式
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int mle=w1;
int mto=t1;
for (int i = 0; i < getChildCount(); i++) {
//测量的宽 高
int width=getChildAt(i).getMeasuredWidth();
int height=getChildAt(i).getMeasuredHeight();
if (mle+w1+width >getWidth() ){
mle=w1;
mto+= height+t1;
getChildAt(i).layout(mle ,mto ,mle+width, mto+height);
}else{
getChildAt(i).layout(mle ,mto ,mle+width, mto+height);
}
mle+=width+w1;
}
}
Activity
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击事件 获取输入框的值
String s = ed.getText().toString();
TextView t = new TextView(MainActivity.this);
t.setText(s);
view.addView(t);
}
});