我試着讓ScrollView像ListView一樣工作。所以每一行都將是一個動態添加的TextView。但程序崩潰。我在LinearLayout中有一個ScrollView。我做錯了什麼?謝謝。在Android上動態添加TextView到ScrollView
這裏是我的代碼
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
請添加您的logcat錯誤。 「程序崩潰」太模糊了。 – Sam