ニャーニャーのブログにゃ

わからないことを調べて公開するブログ

AndroidのRelativeLayoutとは

RelativeLayoutとは

Android Studioで画面レイアウトを編集する際の部品配置方式のひとつで、Viewを他のViewとの相対的な位置関係で記述する方式である。(ViewとはマイクロソフトVisual Studioで言うとコントロールのこと。)

android:layout_below="@id/text_top_left"

のように、

 ・隣のViewの隣接面(上下左右)を表す文字列(例:layout_below)

 ・隣のViewのID

を指定するようになっている。

隣接面を表す文字列には、layout_toEndOfとlayout_toStartOfという紛らわしいものがあるが、これらは、文字を左から右に読む言語の場合は、それぞれ、layout_toRightOf、layout_toLeftOfと同じ効果をもたらす。

 

<感想>

アンドロイドスタジオで使われる用語が、マイクロソフトの開発ツールの用語と違いすぎる。機能上そうならざるを得ないのか、それとも最初から統一する気がないのか。コントロールをビューと言ってみたり、プロパティをアトリビュートと言ってみたり。日本語と英語、マイクロソフトとアンドロイド、いろんな障壁があって学習がなかなか進まない。┐(´д`)┌ヤレヤレ

 

 

f:id:first2018:20210329194117p:plain

```java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/text_top_left"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:textSize="30sp" />

<TextView
android:id="@+id/text_top_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_top_left"
android:layout_marginLeft="10dp"
android:textSize="30sp" />

<TextView
android:id="@+id/text_bottom_left"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_below="@id/text_top_left"
android:layout_marginTop="10dp"
android:textSize="30sp" />

<TextView
android:id="@+id/text_bottom_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text_top_right"
android:layout_toRightOf="@id/text_top_left"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="30sp" />

</RelativeLayout>

```

f:id:first2018:20210329202221p:plain