※Android技術者認定にも役に立ちます!
RelativeLayout
Layout内に特定Viewを基準として自由に配置が可能なLayout。
RelativeLayout重要属性
子ビューに属性を指定する。
◉親を基準に中央・横中央・縦中央に配置する。
↓Sampleコード
layout.xml
※中央
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"
/>
</RelativeLayout >
layout.xml
※横基準中央
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerHorizontal="true"
/>
</RelativeLayout >
layout.xml
※縦基準中央
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerVertical="true"/>
</RelativeLayout >
◉親の左・上・右・下に配置する。
↓Sampleコード
layout.xml
※左(layout_alignParentLeft、layout_alignParentStart)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignParentLeft="true"/>
</RelativeLayout >
layout.xml
※上(layout_alignParentTop)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignParentTop="true"/>
</RelativeLayout >
layout.xml
※右(layout_alignParentRight、layout_alignParentEnd)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignParentRight="true"/>
</RelativeLayout >
layout.xml
※下(layout_alignParentBottom)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignParentBottom="true"/>
</RelativeLayout >
◉指定したビューを基準にビューを配置する。
↓Sampleコード
layout.xml
※左・右を基準にする。
- layout_alignStart、layout_alignEnd
- layout_alignLeft、layout_alignRight
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignStart="@+id/view1"
android:layout_alignEnd="@+id/view1"/>
</RelativeLayout >
layout.xml
※上・下を基準にする。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp
android:background="@color/pink"
android:layout_centerInParent="true"/>
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp
android:layout_alignTop="@+id/view1"
android:background="#000000"
android:layout_alignBottom="@+id/view1"/>
</RelativeLayout >
✳︎追加説明。
◉ android:idとは
Resourceと紐づく。javaファイルでは【R.id.】で紐ついたIDが表示される。
※表示されなかったら!Cleanしてね~
◉ android:backgroundとは
Viewに色をつける。【@color/】or【#からスタートする16進数の6桁の色コード】で設定可能。
※【@color/】は[app]/[res]/[values]/[colors.xml]と紐ついているよ~