?等分して表示する。
レイアウトで、横並びに何個か表示したものを、〇等分して表示する方法です。
横並びに表示するためには、「LinearLayout」を使用します。
LinearLayoutにそのままボタンを入れると、左寄りになってしまいます。
これを、きれいに3等分に表示させたいです。
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
↑を等分して表示させたいものに書きます。
上の画像のボタンを3等分にする場合はこうなります。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ボタン1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ボタン2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ボタン3" />
</LinearLayout>
きれいに3等分できました。
もし、ボタン1と2だけに書き足すと、
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ボタン1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ボタン2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン3" />
</LinearLayout>
ボタン3の大きさは変わらず、ボタン1と2が残った幅で2等分されました。
アプリを作ろう! ANDROID入門 (~ゼロから学ぶアプリの作成から公開まで)
- 作者: WINGSプロジェクト,高江賢,山田祥寛
- 出版社/メーカー: 日経BP社
- 発売日: 2012/09/13
- メディア: 単行本
- 購入: 2人 クリック: 50回
- この商品を含むブログ (11件) を見る