■RelativeLayoutとは
RelativeLayoutとは、基準となるパーツを決め、それとの相対的な関係で位置を決めていくというレイアウトです。
簡単な例
■レイアウトの
右
上
下
左
■Aって言う部品を基準にして位置を決めている。
Aの横
Aの上
Aの下など
そのためにはtextやButton等にIDを割り当てる事が大切。
■レイアウトを基準としてtrueにする。
レイアウトを基準に配置したい場合は、下記の属性を「true」にする
android:layout_alignParentTop : レイアウトの上端
android:layout_alignParentBottom : レイアウトの下端
android:layout_alignParentLeft : レイアウトの左端
android:layout_alignParentRight : レイアウトの右端
android:layout_centerHorizontal : レイアウトの水平方向の中央
android:layout_centerVertical : レイアウトの垂直方向の中央
android:layout_centerInParent : レイアウトの中央
例:
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//レイアウトの中央
android:layout_centerInParent="true"
android:text="Button"/>
■パーツから基準とした位置
基準としたパーツをidで指定し距離を指定する
layout_below : 指定したパーツの下に配置
layout_above : 指定したパーツの上に配置
layout_alignLeft : 指定したパーツの左に配置
ayout_toRightOf : 指定したパーツの右に配置
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="A"/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//aの下側
android:layout_below="@+id/a"
android:text="B"/>
■基準としたパーツやレイアウトからの距離を指定する
layout_alignLeft 左基準 指定したviewと同じ左揃え。
layout_alignRight 右基準 指定したviewと同じ右揃え。
layout_alignTop 上基準 指定したviewと同じ上揃え。
layout_alignBottom 下基準 指定したviewと同じ下揃え。
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:layout_centerInParent="true"
android:text="A"/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//aの下
android:layout_below="@+id/a"
//aと同じ右揃え
android:layout_alignRight="@+id/a"
android:text="B"/>