2012-03-31 13 views
0

main.xml中如何添加行中的Android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <LinearLayout 
    android:id="@+id/horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
     <ImageView 
     android:id="@+id/myImView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:src="@drawable/image1" /> 
    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
     <TableLayout 
     android:id="@+id/myTable" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
     </TableLayout> 
    </ScrollView> 
    </LinearLayout> 

我想表「mytable的」添加n行的表在運行時,我應該怎麼辦?

目前我使用此代碼

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

TableLayout myTable = (TableLayout) findViewById(R.id.myTable); 

TextView tv = new TextView(this); 
for (int x = 0; x < 5; x++) { 
tv.setText("This is row number=" + (x + 1)); 
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT)); 
tr.addView(tv); 
myTable.addView(tr, LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT); 
} 
} 

,但它給我這個錯誤

03-26 00:29:34.070: 
    E/AndroidRuntime(2230): java.lang.RuntimeException: Unable to start activity ComponentInfo{assignment.1/assignment.1.myView}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
+0

只是試試這個myTable.addView(TR);沒有其他參數 – 2012-03-31 16:00:38

回答

2
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
TableLayout myTable = (TableLayout) findViewById(R.id.myTable); 
for (int x = 0; x < 5; x++) { 
TextView tv = new TextView(this); 
tv.setText("This is row number=" + (x + 1)); 
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT)); 
tr.addView(tv); 
myTable.addView(tr, LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT); 
} 
} 

現在就來試試小和微小的變化

0

我想這是因爲你所創建的行中的循環新的每一次反對,但你不用TextView對象來做這件事。

喜歡這個應該工作:

TableLayout myTable = (TableLayout) findViewById(R.id.myTable); 
TextView tv; 
TableRow tr; 
for (int x = 0; x < 5; x++) { 
    tv = new TextView(this); 
    tv.setText("This is row number=" + (x + 1)); 
    tr = new TableRow(this); 
    tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT)); 
    tr.addView(tv); 
    myTable.addView(tr, LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT); 
}