Xamarin.Android RelativeLayout
RelativeLayout
ViewGroup
是顯示子系的View
相對位置中的專案。 的位置 View
可以指定為相對於同層級專案的位置(例如指定元素的左邊或下方),或相對於 的位置 RelativeLayout
區域(例如對齊左下方、置中左方)。
RelativeLayout
是設計使用者介面非常強大的公用程式,因為它可以消除巢狀ViewGroup
的 。 如果您發現自己使用數個巢狀 LinearLayout
群組,您可以將它們取代為單 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>
請注意每個 android:layout_*
屬性,例如 layout_below
、 layout_alignParentRight
與 layout_toLeftOf
。
使用 RelativeLayout
時,您可以使用這些屬性來描述您要如何放置每個 View
。 每個屬性都會定義不同類型的相對位置。 某些屬性會使用同層級 View
的資源標識碼來定義自己的相對位置。 例如,最後Button
一個定義是躺在標識碼所識別ok
的View
左邊和靠上對齊的 (也就是上一個 Button
)。
所有可用的版面配置屬性都會定義在 中 RelativeLayout.LayoutParams
。
請務必在 中載入此版面配置 OnCreate()
方法:
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
}
方法 SetContentView(int)
會載入 資源識別碼所指定的 版面配置檔案 Activity
, Resource.Layout.Main
是指 Resources/Layout/Main.axml 版面設定檔案。
執行應用程式。 您應該會看到下列設定:
資源
此頁面的部分是根據 Android 開放原始碼專案所建立和共用的工作進行修改,並根據 Creative Commons 2.5 屬性授權中所述的詞彙使用。