Android - 无法在LinearLayout中动态显示Textview

时间:2013-04-24 08:48:28

标签: android textview

这是xml代码:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeleteActivity" >
<LinearLayout
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</LinearLayout>

这是java代码的一部分:

    LinearLayout linear_layout = (LinearLayout) findViewById(R.id.linear_layout);
    final TextView[] myTextViews = new TextView[count];
    do {
        myTextViews[pointer] = new TextView(this);
        myTextViews[pointer].setText(getResources().getString(R.string.data_dd) + data + "\n");
        linear_layout.addView(myTextViews[pointer]);
        pointer++;
    }while(c.moveToNext());

问题是只显示第一个textview。如果我使用相对布局而不是线性布局,它会向我显示所有文本视图,但彼此相同。我该怎么办?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

<LinearLayout
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

我认为您应该将LinearLayout的方向更改为verical。另外,TextView你正在创造的还没有LayoutParams

  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

  do {
       myTextViews[pointer] = new TextView(this);
       myTextViews[pointer].setText(getResources().getString(R.string.data_dd) + data + "\n");
       linear_layout.addView(myTextViews[pointer], layoutParams);
       pointer++;
  } while(c.moveToNext());