close

RelativeLayout可以用來設計元件之間具有相對位置的版面配置。在RelativeLayout下的所有元件都可以指定其相對位置。

我們可以利用android:layout_toEndOf屬性,將按鈕02的按鈕放置在按鈕01的按鈕右方,如下圖所示:

右邊.png

原始碼:

<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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/bt01"/>

    <Button
        android:id="@+id/bt02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/bt01"
        android:text="@string/bt02"/>
    
</RelativeLayout>

 

我們可以利用android:layout_toStartOf屬性,將按鈕02的按鈕放置在按鈕01的按鈕左方,如下圖所示:

左邊.png

原始碼:

<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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/bt01"/>

    <Button
        android:id="@+id/bt02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@id/bt01"
        android:text="@string/bt02"/>

</RelativeLayout>

 

我們可以利用android:layout_below屬性,將按鈕02的按鈕放置在按鈕01的按鈕下方,如下圖所示:

下面.png

原始碼:

<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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/bt01"/>

    <Button
        android:id="@+id/bt02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt01"
        android:text="@string/bt02"/>

</RelativeLayout>

 

我們可以利用android:layout_above屬性,將按鈕02的按鈕放置在按鈕01的按鈕上方,如下圖所示:

上面.png

原始碼:

<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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/bt01"/>

    <Button
        android:id="@+id/bt02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bt01"
        android:text="@string/bt02"/>

</RelativeLayout>

 

在上述範例中,我們還設置了讓按鈕01能夠水平置中或垂直置中的屬性。我們可以透過以下幾個屬性來達成:

1. android:layout_centerVertical :
垂直置中,參數是以 true 以及 false 來判斷受否要執行。

2. android:layout_centerHorizontal :
水平置中,參數是以 true 以及 false 來判斷受否要執行。

3. android:layout_centerInParent :
置中於父容器,參數是以 true 以及 false 來判斷受否要執行。

 

還有很多種對齊的屬性:

1. android:layout_alignTop :
元件的 Top 端對齊指定元件的 Top 端,參數是指定元件的ID。

2. android:layout_alignBottom : 
元件的 Bottom 端對齊指定元件的 Bottom 端,參數是指定元件的ID。

3. android:layout_alignStart :
元件的「起始邊界」對齊指定元件的「起始邊界」,參數是指定元件的ID。
LTR語言 中,start 對應 left。
RTL語言 中,start 對應 right。

4. android:layout_alignEnd :
元件的結束邊界對齊指定元件的結束邊界,參數是指定元件的ID。
LTR語言 中,end 對應 right。
RTL語言 中,end 對應 left。

 

這邊需要注意的是 : 
從左到右(LTR)語言:如英文、中文、法文等。此時,start 對應左邊,end 對應右邊。
從右到左(RTL)語言:如阿拉伯文、希伯來文等。此時,start 對應右邊,end 對應左邊。

 

最後~

若有任何錯誤或建議,歡迎大家提出~

arrow
arrow
    創作者介紹
    創作者 Jerry 的頭像
    Jerry

    Android工程師生存筆記

    Jerry 發表在 痞客邦 留言(0) 人氣()