开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜