3

アクティビティでテーブル行を動的に追加しようとしています。表の行は相対レイアウトです。それは問題ないように見えますが、どこが間違っているのかわかりません。以下は私のコードです

 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は次のようになります

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

助けてください。

4

1 に答える 1

9

それTableRowはすでにレイアウトにあるため、クラッシュしています。動的に追加したい場合は、プログラムで作成する必要があります。つまり、次のようになります。

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

relLayout.add(newRow);

実際TableRowには の中で使用する必要がありますTableLayout

何かを複数回使用したい場合は、インフレート テクニックを使用できます。繰り返したい部分 (自分TableRowとその子)だけを含む xml レイアウトを作成する必要があります。

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

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

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

このようにして、元の 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>

次に、次のように膨らませます。

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

それが機能するかどうかを確認してください。

于 2011-03-12T21:07:33.010 に答える