Android常用布局控制項之RelativeLayout

來源:互聯網
上載者:User

我們使用LinearLayout和TableLayout可以滿足開發應用程式介面基本的要求 。但是有時候實現介面的時候不夠靈活,我們還可以使用另外一種控制項 RelativeLayout。RelativeLayout是一種相對布局的控制項,這個容器內部的子元 素們可以使用彼此之間的相對位置或者和容器間的相對位置來進行定位,類似於 網頁設計中的CSS。在指定控制項的位置時,我們需要指定這個控制項與其它控制項之 間的相對位置關係,比如說與另一個控制項的左邊對齊,靠右對齊,位於另一個控制項 的上方,下方等等。一個控制項可以指定與多個其它控制項的相對位置。這樣,我們 就可以在設計位置靈活多變的介面時就會更加的方便。

Android RelativeLayout 屬性

// 相對於給定ID控制項
android:layout_above 將該控制項的底部置於給定ID的控制項之上;
android:layout_below 將該控制項的底部置於給定ID的控制項之下;
android:layout_toLeftOf 將該控制項的右邊緣與給定ID的控制項左邊緣對 齊;
android:layout_toRightOf 將該控制項的左邊緣與給定ID的控制項右邊緣對 齊;
android:layout_alignBaseline 將該控制項的baseline與給定ID的baseline對 齊;
android:layout_alignTop 將該控制項的頂部邊緣與給定ID的頂部邊緣對 齊;
android:layout_alignBottom 將該控制項的底部邊緣與給定ID的底部邊緣對 齊;
android:layout_alignLeft 將該控制項的左邊緣與給定ID的左邊緣對齊;
android:layout_alignRight 將該控制項的右邊緣與給定ID的右邊緣對齊;
// 相對於父組件
android:layout_alignParentTop 如果為true,將該控制項的頂部與其父控制項的 頂部對齊;
android:layout_alignParentBottom 如果為true,將該控制項的底部與其父控制項 的底部對齊;
android:layout_alignParentLeft 如果為true,將該控制項的左部與其父控制項的 左部對齊;
android:layout_alignParentRight 如果為true,將該控制項的右部與其父控制項 的右部對齊;
// 置中
android:layout_centerHorizontal 如果為true,將該控制項的置於水平居 中;
android:layout_centerVertical 如果為true,將該控制項的置於垂直置中;
android:layout_centerInParent 如果為true,將該控制項的置於父控制項的中 央;
// 指定移動像素,值為px
android:layout_marginTop 上位移的值;
android:layout_marginBottom 下位移的值;
android:layout_marginLeft    左位移的值;
android:layout_marginRight   右位移的值;

樣本:

01.<?xml version="1.0" encoding="utf-8"?>02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"03.    android:layout_width="fill_parent"04.    android:layout_height="fill_parent"05.    android:padding="10px"06.    >07.    <TextView08.    android:id="@+id/textView"09.    android:text="TextView"10.    android:layout_width="fill_parent"11.    android:layout_height="wrap_content"12.    />13.    <!--沒有對textView的位置做設定,預設為RelativeLayout容器的左上方  -->14.    <EditText15.    android:id="@+id/editText"16.    android:layout_width="fill_parent"17.    android:layout_height="wrap_content"18.    android:layout_below="@id/textView"19.    android:background="@android:drawable/editbox_background"20.    />21.    <!--editText控制項位於textView控制項的下面-->22.    <Button23.    android:id="@+id/buttonSure"24.    android:text="確定"25.    android:layout_width="wrap_content"26.    android:layout_height="wrap_content"27.    android:layout_below="@id/editText"28.    android:layout_alignParentRight="true"29.    android:layout_marginLeft="10px"30.    />31.    <!--buttonSure控制項在editText控制項的下面,並且於父容器的位置關係為靠右對齊。  android:layout_marginLeft="10px"32.                    設定buttonSure控制項的左外邊距為10像素,即此控制項的左邊與其他控制項相距10像素的距離-->33.    <Button34.    android:id="@+id/buttonCancel"35.    android:text="取消"36.    android:layout_width="wrap_content"37.    android:layout_height="wrap_content"38.    android:layout_toLeftOf="@id/buttonSure"39.    android:layout_alignTop="@id/buttonSure"40.    />41.    <!--buttonCancel為相對於buttonSure的位置確定。buttonCancel控制項的右邊緣與buttonSure控制項的左邊緣對齊,42.                    頂部邊緣與buttonSure控制項的 頂部邊緣對齊-->43.</RelativeLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.