Xamarin.Android RelativeLayout
RelativeLayout
ViewGroup
は、子を表示する です。View
要素を相対位置に配置します。 の位置は、兄弟要素 (特定の View
要素の左または下など) に対する相対位置として、または を基準にした位置で指定できます。RelativeLayout
領域 (中央の下部、左に揃えるなど)。
RelativeLayout
は、入れ子になった を排除できるため、ユーザー インターフェイスを設計するための非常にViewGroup
強力なユーティリティです。 複数の入れ子になったを使用している場合LinearLayout
グループを使用すると、それらを 1 つの RelativeLayout
に置き換えることができます。
HelloRelativeLayout という名前の新しいプロジェクトを開始します。
Resources/Layout/Main.axml ファイルを開き、次を挿入します。
<?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:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
、 などのlayout_below
layout_alignParentRight
各android:layout_*
属性に注目してくださいlayout_toLeftOf
。
を使用する場合は RelativeLayout
、これらの属性を使用して、各 View
を配置する方法を記述できます。 これらの属性はそれぞれ、異なる種類の相対位置を定義します。 一部の属性では、兄弟 View
のリソース ID を使用して、独自の相対位置を定義します。 たとえば、最後Button
の は、ID ok
で識別された の左側に配置され、一番上View
に配置されるように定義されます (これは前Button
の )。
使用可能なすべてのレイアウト属性は、 で RelativeLayout.LayoutParams
定義されています。
でこのレイアウトを読み込みます。OnCreate()
メソッド:
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
}
メソッドはSetContentView(int)
、リソース ID Resource.Layout.Main
で指定された のActivity
レイアウト ファイルを読み込み、Resources/Layout/Main.axml レイアウト ファイルを参照します。
アプリケーションを実行します。 次のレイアウトが表示されます。
リソース
このページの一部は、Android オープン ソース プロジェクトによって作成および共有され、 クリエイティブ コモンズ 2.5 属性ライセンスに記載されている条件に従って使用される作業に基づく変更です。