Android/RelativeLayout練習


#下図コードで作成

コード1

  • は、すべてのTextViewにIDを指定することにより、コード
  • を記述する.
    <?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:gravity="center">
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#FFC4C4" />
    
        <TextView
            android:id="@+id/tv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@+id/tv1"
            android:background="#FFAFAF"/>
    
        <TextView
            android:id="@+id/tv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@+id/tv2"
            android:background="#FA9D9D"/>
    
        <TextView
            android:id="@+id/tv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv1"
            android:background="#FD9191"/>
    
        <TextView
            android:id="@+id/tv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv2"
            android:layout_toEndOf="@+id/tv4"
            android:background="#FB8686"/>
    
        <TextView
            android:id="@+id/tv6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv3"
            android:layout_toEndOf="@+id/tv5"
            android:background="#FA6D6D"/>
    
        <TextView
            android:id="@+id/tv7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv4"
            android:background="#FD6A6A"/>
    
        <TextView
            android:id="@+id/tv8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv5"
            android:layout_toEndOf="@+id/tv7"
            android:background="#F85454"/>
    
        <TextView
            android:id="@+id/tv9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv6"
            android:layout_toEndOf="@+id/tv8"
            android:background="#F33939"/>
    
    </RelativeLayout>

    コード2


    コードを記述するためのIDは、
  • 1のビューにのみ提供される.
    <?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">
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toStartOf="@id/tv5"
            android:layout_above="@id/tv5"
            android:background="#FFC4C4" />
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/tv5"
            android:layout_alignStart="@id/tv5"
            android:background="#FFAFAF"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@id/tv5"
            android:layout_toEndOf="@+id/tv5"
            android:background="#FA9D9D"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignTop="@id/tv5"
            android:layout_toStartOf="@id/tv5"
            android:background="#FD9191"/>
    
        <TextView
            android:id="@+id/tv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:background="#FB8686"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@id/tv5"
            android:layout_alignTop="@id/tv5"
            android:background="#FA6D6D"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv5"
            android:layout_toStartOf="@id/tv5"
            android:background="#FD6A6A"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/tv5"
            android:layout_alignStart="@id/tv5"
            android:background="#F85454"/>
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/tv5"
            android:layout_toEndOf="@id/tv5"
            android:background="#F33939"/>
    
    </RelativeLayout>

    💡新知


    プロパティのレイアウトされていないtoStartOfは、ビューの終了エッジとターゲットビューの開始エッジを直線的に維持します.Layout toEndOfは、このビューのstartedgeとターゲットビューのendedgeを直線的に維持します.Layout superは、このビューの下端とターゲットビューの上端を直線上に配置します.Layout newは、このビューのtopedgeとターゲットビューのunderedgeを直線上に配置します.
    プロパティのレイアウトされていないalignStartは、ビューのstartedgeとターゲットビューのstartedgeを直線的に維持します.Layout alignEndは、ビューのendedgeとターゲットビューのendedgeを直線上に配置します.Layout alignTopは、ビューのtopedgeとターゲットビューのtopedgeを直線上に配置します.Layout alignBottomは、ビューの底辺とターゲットビューの底辺を直線に保つ.