动态创建一个新的TextView,然后在另一个TextView下方显示它。

50
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
    TextView tv=new TextView(getApplicationContext());
    tv.setText(textArray[i]);
    relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
    layout.addView(tv, relativeParams);
}

我需要做类似于这样的事情.. 这样它会显示为

one
two
asdfasdfsomething

在屏幕上显示...


1
简单来说,你的代码有什么问题?哪里不起作用?顺便说一句,如果这是一个Activity,只需使用“this”而不是“getApplicationContext()”。 - EboMike
你的问题标题说“在另一个TextView下方显示它”,但是你的问题陈述不同,所有文本都在一行中 - 请澄清或相应地格式化问题文本。 - Mathias Conradt
问题的格式不正确 - 我已经修复了。 (可能不直观,换行符被吞噬了) - EboMike
使用线性布局(LinearLayout)代替相对布局(RelativeLayout)。 - Vishal Patoliya ツ
4个回答

77

如果使用RelativeLayout并不重要,您可以使用LinearLayout,并执行以下操作:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

这样做可以避免你尝试过的addRule方法。你可以简单地使用addView()来添加新的TextView。

完整代码:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);        
for( int i = 0; i < textArray.length; i++ )
{
    TextView textView = new TextView(this);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

6
不要使用应用程序上下文来创建视图。请使用活动上下文。 - bryn

23

试试这段代码:

final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];

for (int i=0; i<str.length; i++)
{
    tv[i] = new TextView(this); 
    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
         ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
    params.leftMargin = 50;
    params.topMargin  = i*50;
    tv[i].setText(str[i]);
    tv[i].setTextSize((float) 20);
    tv[i].setPadding(20, 50, 20, 50);
    tv[i].setLayoutParams(params);
    rl.addView(tv[i]);  
}

我该如何在activity_main.xml文件中定义rl? - gimmegimme

18
public View recentView;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Create a relative layout and add a button
        relativeLayout = new RelativeLayout(this);
        btn = new Button(this);
        btn.setId((int)System.currentTimeMillis());
        recentView = btn;
        btn.setText("Click me");
        relativeLayout.addView(btn);


        setContentView(relativeLayout);

        btn.setOnClickListener(new View.OnClickListener() {

            @Overr ide
            public void onClick(View view) {

                //Create a textView, set a random ID and position it below the most recently added view
                textView = new TextView(ActivityName.this);
                textView.setId((int)System.currentTimeMillis());
                layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
                textView.setText("Time: "+System.currentTimeMillis());
                relativeLayout.addView(textView, layoutParams);
                recentView = textView;
            }
        });
    }

这可以被修改为在不同的TextViews中显示String数组的每个元素。


我在使用这个逻辑处理多个视图时遇到了问题,因为多个项目被分配了相同的ID,我用计数器替换了当前时间来解决这个问题。 - Roberto

4

您没有为文本视图分配任何ID,但是却使用 tv.getId() 将其作为参数传递给 addRule 方法。请尝试通过 tv.setId(int) 设置唯一的ID。

实际上,您还可以使用垂直方向的LinearLayout,这可能更容易一些。如果不必要,我更喜欢LinearLayout而不是RelativeLayouts。


你如何在程序中分配一个唯一的ID?我的意思是,当你不知道XML中ID的值以及将要出现的ID时,如何确保它是唯一的。 - Dany Y
你会知道xml中的id值,为什么不呢?像thisKindofElem001thisKindofElem002这样设置id。 - fiatjaf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接