行充气器只添加一次行

时间:2013-09-29 16:37:44

标签: android row layout-inflater

今天早上我花了相当多的钱试图学习如何用同一行多次充气活动。我创建了一个名为row per xren的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次的行只显示一次。我做错了什么。

2 个答案:

答案 0 :(得分: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
 }

答案 1 :(得分:0)

以下是您的回答:Use view to inflate multiple times

相关问题