Android のドキュメントで、この奇妙な重みの値についていつも読んでいます。今初めて試してみたいのですが、まったく機能しません。
ドキュメントから理解したところによると、このレイアウトは次のようになります。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Register"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
<Button
android:text="Not this time"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
weight="1" />
</LinearLayout>
水平に並んでスペースを均等に共有する 2 つのボタンを作成する必要があります。問題は、2 つのボタンがスペースを埋めるまで拡大しないことです。
ボタンを大きくして行全体を埋めたいと思います。両方のボタンが親と一致するように設定されている場合、最初のボタンのみが表示され、行全体を埋めます。
ベストアンサー1
覚えておくべき3つのこと:
- 子のandroid:layout_widthを「0dp」に設定する
- 親のandroid:weightSumを設定します(編集: Jason Moore が指摘したように、この属性はオプションです。デフォルトでは子の layout_weight の合計に設定されています)
- 各子のandroid:layout_weight を比例的に設定します(例: weightSum="5"、子が 3 名の場合: layout_weight="1"、layout_weight="3"、layout_weight="1")
例:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
そして結果は次の通りです。