动态添加TextView

LinearLayout中没有TextView对象
动态添加
text1=new TextView(this);
text1.setText(“动态添加”);
((LinearLayout) this.findViewById(R.id.layout)).addView(text1);

控制布局,可以通过RelativeLayout.LayoutParams类
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.groups);
final TextView tv1 = new TextView(this);
tv1.setText(“常用联系人”);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.BELOW, R.id.groups);
tv1.setLayoutParams(lp1);
linearLayout.addView(tv1, lp1);

也可采用linearLayout.addView(tv1, 0); // 线性布局 通过参数index控制加入的控件的位置

你可能感兴趣的:(java)