如何在Runtime中添加TextView?[英] How to add TextView in Runtime?

本文是小编为大家收集整理的关于如何在Runtime中添加TextView?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何在运行时添加新TextView到布局?是否有可能?

推荐答案

完整解决方案:

    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);

其他推荐答案

以编程方式添加TextView(或一般)到布局(或一般的ViewGroup)是可以检查视图群的public void addView (View child, int index)方法.

本文地址:https://www.itbaoku.cn/post/102475.html

问题描述

How to add a new TextView to a layout in 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.