动态添加TextView

来源:互联网 发布:java爬虫技术视频教程 编辑:程序博客网 时间:2024/06/10 15:05

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控制加入的控件的位置

0 0
原创粉丝点击