1

我正在使用 TableLayout,其中第一行是静态的 (XML),第二行是在运行时添加的。两种布局在视觉上应该是相同的。但是,我得到了不同的结果:

ROW 1: [----A-----] [----------B----------] [-----C----]
ROW 2: [-----A-----] [---------B---------] [-----C-----]

这是第 1 行的 XML:

<Button
 android:id="@+id/stat_A"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:text="A" />

按钮 stat_A 和 stat_C 的权重为“1”,而 stat_B 的权重为“2”。

这是 ROW 2 的 Java 代码:

 Button btn = new Button(this);
 btn.setId("dyn_A");
 LayoutParams btn_param = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, _DYN_WEIGHT_);
 btn.setLayoutParams(btn_param);
 btn.setText("A");
 layout.addView(btn);

_DYN_WEIGHT_ 对于按钮 dyn_A 和 dyn_C 是“1”,对于 dyn_B 是“2”。

我的错误在哪里?

谢谢!

编辑

完整的 XML:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Tbl01"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <TableRow
        android:id="@+id/row1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">  

        <Button
            android:id="@+id/stat_A"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="A" />

        <Button
            android:id="@+id/stat_B"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="B" />

        <Button
            android:id="@+id/stat_C"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="C" />

    </TableRow>     


    <TableRow
        android:id="@+id/row2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
    </TableRow>

</TableLayout>  
4

0 回答 0