Android - 静态和动态按钮重量之间的差异

时间:2013-04-05 12:51:23

标签: java android layout

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

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

以下是ROW 1的XML:

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

重量是&#34; 1&#34;对于按钮stat_A和stat_C,以及&#34; 2&#34;对于stat_B。

这是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_是&#34; 1&#34;对于按钮dyn_A和dyn_C,以及&#34; 2&#34;对于dyn_B。

我的错误在哪里?

谢谢!

修改

完整的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>  

0 个答案:

没有答案
相关问题