2013-09-29 27 views
0

今天早上我花了相当多的钱试图学习如何用同一行多次膨胀一个活动。我创建了一个名为row per person的xml文件。行充气机只添加行一次

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <EditText 
     android:id="@+id/enterName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:inputType="textPersonName" > 

     <requestFocus /> 
    </EditText> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" /> 

    <TextView 
     android:id="@+id/tipPerPerson" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="$  " /> 
</TableRow> 

而且我的活动BILL_ENTRY_SCREEN,我进入了下面的代码。

 rowPerPerson = (TableRow) findViewById(R.id.divideTips); 

    TableLayout table = (TableLayout)BILL_ENTRY_SCREEN.this.findViewById(R.id.table); 

    for(int i = 1; i <4; i++) { 
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.row_per_person, table); 
    } 

然而,而不是行显示了4倍,它只显示一次。我究竟做错了什么。

回答

1

对于每个i,您必须为TableRow视图创建一个新实例,然后将其添加到TableLayout

所以,你的代码应该是这样的:

for(int i = 1; i <=4; i++) { 
    row = new TableRow(this); 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.row_per_person, table); 
    row.addView(inflater);  // add view to row 
    table.addView(row); // add to table 
} 
+0

我得到一个错误说膨胀不能被解析为一个变量。我改变它inflater,但是当得到一个错误消息,它需要一个视图 – Aaron

+0

@Aaron我编辑答案。请检查它并让我知道结果。 – mt0s