0

在这里,我必须根据数组列表大小以编程方式添加文本视图。文本视图应该像继续模式一样出现在行中......例如。tv1、tv2、tv3 等直到数组列表的大小。

但是在这里我得到了彼此出现的文本视图。我看不懂他们的文字。这是我的代码:

ArrayList<String> languageNames = new ArrayList<String>();
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
if(languageNames.size()>0)
{
    int size = languageNames.size();
    TextView[] tv = new TextView[size];
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.BELOW, tvLocation.getId());

    for(int i=0; i<size; i++)
    {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText(languageNames.get(i).toString());
        tv[i].setLayoutParams(p);
        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
    }
}
else
{

}

需要做什么才能以适当的方式获得文本视图?

4

2 回答 2

1

在 a 中添加按钮LinearLayout并将其添加LinearLayoutRelativeLayout.

RelativeLayout r1 = (RelativeLayout) findViewById(R.id.r1);
 RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
LinearLayout LL = new LinearLayout(getBaseContext());
LL.setOrientation(LinearLayout.VERTICAL);

   for (int i=0;i< size;i++) {
     tv[i] = new TextView(getBaseContext());
    tv[i].setText(languageNames.get(i).toString());

    tv[i].setPadding(50, 50, 0, 0);
    tv[i].setTextColor(Color.parseColor("#000000"));
     LL.addView(tv);   
 }
r1.addview(LL, p);
于 2013-09-10T07:51:16.043 回答
0

试试这个代码:

LinearLayout rl = (LinearLayout)findViewById(R.id.mainLayout);

    TextView[] tv = new TextView[10];
    for(int i=0; i<10; i++)
    {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText("TextView "+ i);

        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
    }

希望对你有帮助

于 2013-09-10T07:58:50.850 回答