创建一个新的TextView编程然后显示在下面的另一个TextView的 [英] create a new textview programmatically then display it below another textview

查看:652
本文介绍了创建一个新的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()来添加新的TextViews。

Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.

完成code:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆