如何从XML文件动态地添加视图,我想补充的TableRow在RelativeLayout的我想、视图、文件、动态

2023-09-05 10:04:42 作者:盗心人

我想在我的活动动态添加表行。表行是在相对布局。它看起来不错,但不知道我要去的地方错了。下面是我的code

I am trying to add table row dynamically in my activity. The table row is in the relative layout. It looks fine but don't know where I am going wrong. Below is my code

 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
}

而main.xml中如下:

And the main.xml as follows

  <?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>

请帮助。

推荐答案

它的崩溃,因为的TableRow 已经在布局。如果要动态地添加一些你必须以编程方式创建它,那就是:

It's crashing because that TableRow is already in the layout. If you want to add some dynamically you have to create it programmatically, that is:

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

relLayout.add(newRow);

实际上的TableRow 应该在 TableLayout 被使用。

如果你想使用更多的东西比一次,就可以使用充气技术。您需要创建一个XML布局,其中包括要重复只有部分(所以你的的TableRow 及其子),然后输入:

If you want to use something more than once, you can use the inflate technique. You need to create an xml layout that includes the only part that you want to repeat (so your TableRow and its children), and then:

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

View inflated = inflater.inflate(R.layout.your_layout, null);

现在充气包含您所指定的布局。相反,你可能想要把那里它附加膨胀的一个布局。你需要这样的新元素每一次,你将不得不抬高用同样的方式。

Now inflated contains the layout you specified. Instead of null, you might want to put there the layout to which attach the inflated one. Every time you need a new element like that, you would have to inflate it the same way.

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

(You should always report the error you get when it crashes)

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

好了,现在我明白了,这是你的code:

ok now i see, this is your code:

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);
}

这样你夸大你的整个布局原来的TableRow里面。

this way you're inflating your whole layout inside the original TableRow.

您应该有一个这样的 row.xml 布局,加上的main.xml

You should have a row.xml layout like this, together with the 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>

和再膨胀它是这样的:

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);
}

看看是否可行。

see if it works.