public class MainActivity extends Activity {
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//代码添加布局文件
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(params);
TextView textview = new TextView(this);
textview.setText("hello");
@Override
public void onClick(View v) {
//获取其父View对象v就是点击事件的那个对象对于这里是TextView
ViewGroup parent = (ViewGroup) v.getParent();
TextView textview = new TextView(MainActivity.this);
textview.setText("我是动态生成的"+(i++));
//添加一事件点击删除
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
}
});
parent.addView(textview);
}
//这是个重载的方法
this.setContentView(linearLayout);
}
}
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//代码添加布局文件
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
//设置布局的填充方式
linearLayout.setLayoutParams(params);
TextView textview = new TextView(this);
textview.setText("hello");
textview.setTextSize(30);
@Override
public void onClick(View v) {
//获取其父View对象v就是点击事件的那个对象对于这里是TextView
ViewGroup parent = (ViewGroup) v.getParent();
TextView textview = new TextView(MainActivity.this);
textview.setText("我是动态生成的"+(i++));
//添加一事件点击删除
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
parent.removeView(v);
}
});
parent.addView(textview);
}
});
//这是个重载的方法
this.setContentView(linearLayout);
}
}