0

Android のテーブル レイアウトに行を動的に追加しようとしています (このチュートリアルを参照しました: http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout )。ただし、これは間違ったビュー階層につながっています。コードと出力画像を添付しました。

XML ファイル:

<TableLayout
    android:id="@+id/task_table"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*" >
    <TableRow
        android:id="@+id/task_heading_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/task_heading_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="Task"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="2"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="3"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="4"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="5"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="6"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="7"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="8"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>
</TableLayout>

Java コード:

 TableRow row = new TableRow(this);
 row.setLayoutParams(new TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT)); 
 TextView t1 = new TextView(this);
 t1.setLayoutParams(new TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT));

 String heading = "heading";
 t1.setText(heading);
 row.addView(t1);
 for (int i=0;i<8;i++){
     TextView view = new TextView(this);

     view.setLayoutParams(new TableRow.LayoutParams(
             TableRow.LayoutParams.MATCH_PARENT,
             TableRow.LayoutParams.WRAP_CONTENT));
     view.setText("child");
     row.addView(view);
 }
 task_table.addView(row, new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

出力では、ループに追加しているテキスト「子」を含む8つのビューすべてが、列ごとに1つの「子」ではなく、1というタイトルの列に追加されます。

期待される出力:

Task |     1 |     2 |     3 |      4 |     5 |    6 |     7 |     8 

heading | child | child | child | child | child | child | child | child

実際の出力:

Task |     1 |     2 |     3 |      4 |     5 |    6 |     7 |     8 

heading | child child child child child child child child | | | | | | | 

誰かが私を助けて、私が間違っていることを教えてもらえますか? どんな助けでも大歓迎です。

4

1 に答える 1

0

誰かが私を助けて、私が間違っていることを教えてもらえますか?

プログラムで追加された8つすべてが列にTextViews入るとは思えません。1おそらく、6つが見出し列に、2つが1列に入るでしょう。

レイアウトではlayout_span、すべてのプロパティを使用します。TextViewsこれは、それぞれTextViewsが6つの通常の列に広がるように設定されていることを意味します。これTextviewsで、コードにそれらを追加すると、一度に1つの列に入力されて追加されるため(拡散なし)、最終的に1対1の一致は得られません。

layout_spanレイアウトからプロパティを削除するTextViewsか、何らかの奇妙な理由で、コードで追加しlayout_spanたものに同じプロパティを追加できない場合(を介して)。TextViewsTableRow.LayoutParams

TableRowまた、とその子には特別な制約があるため、これらのlayout_width / heightパラメーターをすべて指定する必要はありません(したがって、これらの値は役に立ちません)。がコンテンツを拡大して幅を埋めるように設定されている場合、コンテンツを中央にandroid:gravity="center_horizontal"配置するように指示すると、これも役に立ちません。TableRowTableLayout

于 2013-03-23T18:52:44.303 に答える