0
0

More than 3 years have passed since last update.

【AndroidStudio】RelativeLayout

Posted at

■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"/>

スクリーンショット 2021-05-30 20.45.19.png

■パーツから基準とした位置

基準としたパーツを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"/>

スクリーンショット 2021-05-30 20.56.59.png

■基準としたパーツやレイアウトからの距離を指定する

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"/>

スクリーンショット 2021-05-30 21.04.15.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0