开发者

How to add TextView in Runtime?

开发者 https://www.devze.com 2023-01-20 19:04 出处:网络
How to add a new TextView to a layout in开发者_StackOverflow中文版 run time? is it possible?Complete solution:

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

精彩评论

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