6

ビューを TableLayout に追加しようとしていますが、単に表示されません (エミュレーターでテストしています)。実際、私はこれよりも多くのコードを書きました。つまり、テーブル行にテキストビューを追加しましたが、これまでに削減しましたが、これでも機能しません。理由はありますか?

のコードは次のtest.xmlとおりです。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout"
    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" >
</TableRow>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST" >
    </TextView>
</TableRow>

</TableLayout>

そしてJavaコード:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);

    /* Find Tablelayout defined in test.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.tableLayout);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    /* Create a Button to be the row-content. */
    Button b = new Button(this);
    b.setText("Dynamic Button");
    b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    /* Add Button to row. */
    tr.addView(b);
    /* Add row to TableLayout. */
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
}

エミュレーターでテストすると、ハードコーディングされた文字列「TEST」(xml ファイルで定義) を含むテキストビューのみが表示されます。

4

1 に答える 1

13

TableLayout.LayoutParamsテーブルに行を追加するときは、buttonLayoutを設定したり、を追加したりしないでください。

//b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(b);
//tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
t1.addView(tr)

TableLayout.LayoutParamsいくつかの問題を引き起こしていると思います。

于 2012-05-17T16:46:35.137 に答える