AndroidのRelativeLayoutのコードをどのように書けば良いですか?
Androidの相対レイアウトコードは、次の手順に従って記述できます。
- 布局ファイルでルートレイアウトとして相対レイアウトを選択してください。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
- 子ビューを追加し、相対位置と属性を設定してください。
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginTop="16dp"
android:layout_alignParentTop="true"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true"
/>
上記のコードでは、textView1のトップが親レイアウトのトップに揃えられ、上部に16dpのマージンが追加されます。button1はtextView1の下に配置され、水平方向に中央揃えされます。
- 根布局タグを閉じる。
</RelativeLayout>
これは相対レイアウトのコードの簡単な例です。必要に応じて、さらに多くのサブビューを追加し、相対レイアウトのルールに従って配置することができます。