AndroidでViewを画面いっぱいに配置する方法
本稿ではAndroidアプリ開発において、アプリ上で画面いっぱいにViewを表示する方法を説明します。
match_parentを使用する方法
例えば、1つのLinearLayoutの中に3つのボタンを入れるようなレイアウトがあるとします。
その3つのボタンで画面全体を埋めるには次のレイアウトを書きます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
それぞれのボタンのwidthとheight属性を“match_parent“にし、
weight属性を“1”に設定することで同じ幅と高さでボタンが画面いっぱいに表示されます。
スポンサーリンク