相対レイアウト
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Compose を試す
Jetpack Compose は、Android に推奨される UI ツールキットです。Compose でレイアウトを操作する方法を学習します。
<ph type="x-smartling-placeholder">
</ph>
Compose の ConstraintLayout →
RelativeLayout
は、子ビューを相対位置に表示するビューグループです。各ビューの位置は、兄弟要素に対する相対位置(別のビューの左側や下など)として指定することも、親の RelativeLayout
領域に対する相対位置(下揃え、左揃え、中央揃えなど)で指定することもできます。
注: パフォーマンスやツールのサポートを改善するには、ConstraintLayout を使用してレイアウトを作成する必要があります。
RelativeLayout
は、ユーザー インターフェースを設計するための非常に強力なユーティリティです。このユーティリティを使用すると、ネストされたビューグループを排除し、レイアウト階層をフラットに保つことで、パフォーマンスを改善できます。複数のネストされた LinearLayout
グループを使用している場合、それらを 1 つの RelativeLayout
で置換できることがあります。
ビューの配置
RelativeLayout
を使用すると、子ビューの親ビューに対する相対位置を指定することも、相互(ID で指定)の相対位置を指定することもできます。そのため、2 つの要素を右枠線で整列したり、画面中央や左中央などで上下に並べたりできます。デフォルトでは、すべての子ビューがレイアウトの左上に描画されるため、RelativeLayout.LayoutParams
で使用できる各種のレイアウト プロパティを使って各ビューの位置を定義する必要があります。
RelativeLayout
のビューで使用できる各種のレイアウト プロパティの一部を以下に示します。
android:layout_alignParentTop
-
"true"
の場合、このビューの上端が親の上端と一致するように配置されます。
android:layout_centerVertical
-
"true"
の場合、この子が親内の縦方向中央に配置されます。
android:layout_below
- リソース ID で指定されたビューの下にこのビューの上端が来るように配置されます。
android:layout_toRightOf
- リソース ID で指定されたビューの右側にこのビューの左端が来るように配置されます。
これらはほんの一例です。すべてのレイアウト属性が RelativeLayout.LayoutParams
に記載されています。
各レイアウト プロパティの値には、親の RelativeLayout
に対する相対的なレイアウト位置を有効にするブール値か、ビューを配置するレイアウト内の別のビューを参照する ID のいずれかを指定します。
XML レイアウトでは、レイアウト内の他のビューに対する依存関係を任意の順序で宣言できます。
たとえば、「view2」が階層内で宣言されている最後のビューでも、「view1」を「view2」の下に配置するように宣言できます。次の例は、このようなシナリオを示しています。
例
各ビューの相対位置を制御する各属性が太字で示されています。
<?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:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
RelativeLayout
の各子ビューで使用できるすべてのレイアウト属性の詳細については、RelativeLayout.LayoutParams
をご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Relative Layout\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to work with layouts in Compose. \n[ConstraintLayout in Compose →](/jetpack/compose/layouts/constraintlayout) \n\n[RelativeLayout](/reference/android/widget/RelativeLayout) is a view group that displays child views in relative\npositions. The position of each view can be specified as relative to sibling elements (such as to\nthe left-of or below another view) or in positions relative to the parent [RelativeLayout](/reference/android/widget/RelativeLayout) area (such as aligned to the bottom, left or center).\n\n**Note:**\nFor better performance and tooling support, you should instead [build your layout with ConstraintLayout](/training/constraint-layout).\n\nA [RelativeLayout](/reference/android/widget/RelativeLayout) is a very powerful utility for designing a user interface\nbecause it can eliminate nested view groups and keep your layout hierarchy flat, which improves\nperformance. If you find yourself using several nested [LinearLayout](/reference/android/widget/LinearLayout) groups,\nyou may be able to replace them with a single [RelativeLayout](/reference/android/widget/RelativeLayout).\n\nPositioning Views\n-----------------\n\n[RelativeLayout](/reference/android/widget/RelativeLayout) lets child views specify their position relative to the\nparent view or to each other (specified by ID). So you can align two elements by right border, or\nmake one below another, centered in the screen, centered left, and so on. By default, all child\nviews are drawn at the top-left of the layout, so you must define the position of each view\nusing the various layout properties available from [RelativeLayout.LayoutParams](/reference/android/widget/RelativeLayout.LayoutParams).\n\nSome of the many layout properties available to views in a [RelativeLayout](/reference/android/widget/RelativeLayout)\ninclude:\n\n[`android:layout_alignParentTop`](/reference/android/widget/RelativeLayout.LayoutParams#attr_android:layout_alignParentTop)\n: If `\"true\"`, makes the top edge of this view match the top edge of the parent.\n\n[`android:layout_centerVertical`](/reference/android/widget/RelativeLayout.LayoutParams#attr_android:layout_centerVertical)\n: If `\"true\"`, centers this child vertically within its parent.\n\n[`android:layout_below`](/reference/android/widget/RelativeLayout.LayoutParams#attr_android:layout_below)\n: Positions the top edge of this view below the view specified with a resource ID.\n\n[`android:layout_toRightOf`](/reference/android/widget/RelativeLayout.LayoutParams#attr_android:layout_toRightOf)\n: Positions the left edge of this view to the right of the view specified with a resource ID.\n\nThese are just a few examples. All layout attributes are documented at [RelativeLayout.LayoutParams](/reference/android/widget/RelativeLayout.LayoutParams).\n\nThe value for each layout property is either a boolean to\nenable a layout position relative to the parent [RelativeLayout](/reference/android/widget/RelativeLayout) or an ID that\nreferences another view in the layout against which the view should be positioned.\n\nIn your XML layout, dependencies against other views in the layout can be declared in any order.\nFor example, you can declare that \"view1\" be positioned below \"view2\" even if \"view2\" is the last\nview declared in the hierarchy. The example below demonstrates such a scenario.\n\nExample\n-------\n\nEach of the attributes that control the relative position of each view are emphasized. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cRelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:paddingLeft=\"16dp\"\n android:paddingRight=\"16dp\" \u003e\n \u003cEditText\n android:id=\"@+id/name\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:hint=\"@string/reminder\" /\u003e\n \u003cSpinner\n android:id=\"@+id/dates\"\n android:layout_width=\"0dp\"\n android:layout_height=\"wrap_content\"\n android:layout_below=\"@id/name\"\n android:layout_alignParentLeft=\"true\"\n android:layout_toLeftOf=\"@+id/times\" /\u003e\n \u003cSpinner\n android:id=\"@id/times\"\n android:layout_width=\"96dp\"\n android:layout_height=\"wrap_content\"\n android:layout_below=\"@id/name\"\n android:layout_alignParentRight=\"true\" /\u003e\n \u003cButton\n android:layout_width=\"96dp\"\n android:layout_height=\"wrap_content\"\n android:layout_below=\"@id/times\"\n android:layout_alignParentRight=\"true\"\n android:text=\"@string/done\" /\u003e\n\u003c/RelativeLayout\u003e\n```\n\nFor details about all the layout attributes available to each child view of a [RelativeLayout](/reference/android/widget/RelativeLayout), see [RelativeLayout.LayoutParams](/reference/android/widget/RelativeLayout.LayoutParams)."]]