How to add TextView in Runtime?
How to add a new TextView to a layout in开发者_StackOverflow中文版 run time? is it possible?
Complete solution:
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);
Programmatically adding TextView
(or View
in general) to layout (or ViewGroup
in general) is possible, check ViewGroup's public void addView (View child, int index)
method.
精彩评论