私は TableLayout を使用しています。最初の行は静的 (XML) で、2 番目の行は実行時に追加されます。両方のレイアウトは視覚的に同等である必要があります。ただし、異なる結果が得られます。
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" />
重みは、ボタン 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>