以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方 [英] Create a new TextView programmatically then display it below another TextView
本文介绍了以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
}
我需要做类似的事情..所以它会显示为
I need to do something like that.. so it would display as
one
two
asdfasdfsomething
在屏幕上..
推荐答案
如果使用 RelativeLayout 并不重要,您可以使用 LinearLayout,并这样做:
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
这样做可以避免您尝试过的 addRule 方法.您可以简单地使用 addView() 添加新的 TextView.
Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.
完整代码:
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);
}
这篇关于以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文