如何从xml文件动态添加视图,我想在RelativeLayout中添加TableRow

Sha*_*aaz 3 android tablerow

我试图在我的活动中动态添加表行.表行处于相对布局中.它看起来很好,但不知道我哪里出错了.以下是我的代码

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
    TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
    for(int i = 1; i <3; i++)
        RLayout.addView(tableRow); //My code is crashing here
}
Run Code Online (Sandbox Code Playgroud)

而main.xml如下

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
  android:id="@+id/RelativeLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  >
  <TableRow
  android:id="@+id/TableRow"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_alignParentTop="true"
  android:layout_alignParentLeft="true"
  >
  <TextView
  android:id="@+id/Text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Text"
  >
  </TextView>
  </TableRow>
  </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请帮忙.

big*_*nes 9

它崩溃了,因为它TableRow已经在布局中了.如果要动态添加一些,则必须以编程方式创建它,即:

// PSEUDOCODE
TableRow newRow = new TableRow(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(/*....*/);
newRow.setLayoutParams(lp);

relLayout.add(newRow);
Run Code Online (Sandbox Code Playgroud)

其实TableRow应该在里面使用TableLayout.

如果您想多次使用某些东西,可以使用膨胀技术.您需要创建一个xml布局,其中包含您想要重复的唯一部分(TableRow以及您和它的孩子),然后:

LayoutInflater inflater = 
              (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View inflated = inflater.inflate(R.layout.your_layout, null);
Run Code Online (Sandbox Code Playgroud)

现在inflated包含您指定的布局.而不是null,你可能想要放置附加膨胀的布局.每当你需要一个像这样的新元素时,你就必须以同样的方式给它充气.

(你应该总是报告崩溃时得到的错误)

------编辑-----

好吧,我现在看,这是你的代码:

RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
TableRow tableRow = (TableRow)findViewById(R.id.TableRow);

for(int i = 1; i <4; i++) {
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View inflated = inflater.inflate(R.layout.main, tableRow);
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以在原始的TableRow中扩展你的整个布局.

你应该有这样的row.xml布局,以及main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/TableRow"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_alignParentTop="true"
  android:layout_alignParentLeft="true"
  >
  <TextView
  android:id="@+id/Text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Text"
  />
  </TableRow>
Run Code Online (Sandbox Code Playgroud)

然后像这样膨胀它:

RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);

for(int i = 1; i <4; i++) {
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.row, RLayout);
}
Run Code Online (Sandbox Code Playgroud)

看看它是否有效.