私は水平線で3つのボタン(それぞれ同じ幅)を持つレイアウトを作ろうとしています。Androidでグリッドのようなレイアウトを作る方法
xxxx xxxx xxxx
xxxx xxxx xxxx
Xはボタンを表します。どのように各セルに33%を分配し、その中に伸びないボタンを追加できますか?
私は水平線で3つのボタン(それぞれ同じ幅)を持つレイアウトを作ろうとしています。Androidでグリッドのようなレイアウトを作る方法
xxxx xxxx xxxx
xxxx xxxx xxxx
Xはボタンを表します。どのように各セルに33%を分配し、その中に伸びないボタンを追加できますか?
一つのアプローチはandroid:layout_weight="1"
で、LinearLayout
を使用することです:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:text="Button 1"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="Button 2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="Button 3"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
TableLayout
:3列2行、stretchModeをspacingWidthUniform
とすることができます。
編集:試していないが、これはうまくいくはずだ。
<TableLayout android:id="@+id/TableLayout01"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:stretchMode="spacingWidthUniform">
<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Settings"
android:id="@+id/btnSettings"></Button>
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button02" android:id="@+id/Button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="@+id/Button03" android:id="@+id/Button03"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button04" android:id="@+id/Button04"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button05" android:id="@+id/Button05"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>