コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

Androidアプリ入門 No.34 LinearLayoutの要素の配置指定

LinearLayout

要素の配置指定

次に、レイアウトの中の配置を決める指定の属性を確認する。

属性 説明
gravity レイアウトの中の配置を決定する。

gravity属性は以下の値が指定できる。

説明
top コンテナの一番上に配置される。サイズは変更されない。
bottom コンテナの一番下に配置される。サイズは変更されない。
left コンテナの一番左に配置される。サイズは変更されない。
right コンテナの一番右に配置される。サイズは変更されない。
center_vertical コンテナの縦方向の真ん中に配置される。サイズは変更されない。
fill_vertical 高さをコンテナの高さにする。
center_horizontal コンテナの横方向の真ん中に配置される。サイズは変更されない。
fill_horizontal 幅をコンテナの幅にする。
center コンテナの真ん中に配置される。サイズは変更されない。
fill 高さと幅を、コンテナの高さと幅にする。
clip_vertical topとbottomのオプションとして、上部もしくは下部をコンテナの境界にする。
clip_horizontal leftとrightのオプションとして、左もしくは右をコンテナの境界にする。

main.xmlを以下のようにして確認する。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="240dip"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="160dip"
            android:layout_height="fill_parent"
            android:background="#f00"
            >
            <TextView
                android:text="あいうえお"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textColor="#000"
                android:gravity="bottom"
                />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="160dip"
            android:layout_height="fill_parent"
            android:background="#0f0"
            >
            <TextView
                android:text="かきくけこ"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textColor="#000"
                android:gravity="center_vertical|center_horizontal"
                />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="240dip"
        android:background="#00f"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="160dip"
            android:layout_height="fill_parent"
            android:background="#00f"
            >
            <TextView
                android:text="さしすせそ"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textColor="#000"
                android:gravity="bottom|right"
                />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="160dip"
            android:layout_height="fill_parent"
            android:background="#ff0"
            >
            <TextView
                android:text="たちつてと"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textColor="#000"
                android:gravity="center"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

実行結果は以下。