如何在运行时向布局添加新的TextView?有可能吗?
答案 0 :(得分:5)
完整的解决方案:
View parent; //parent view where to add
ViewGroup layout=new LinearLayout(context);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(context);
tv1.setText("Test1");
TextView tv2=new TextView(context);
tv2.setText("Test2");
layout.addView(tv1);
layout.addView(tv2);
parent.addView(layout);
答案 1 :(得分:1)
可以通过编程方式将TextView
(或一般View
)添加到布局(或一般ViewGroup
),查看ViewGroup的public void addView (View child, int index)
方法。