Android ApiDemos樣本解析(151):Views->Layouts->Rela

來源:互聯網
上載者:User

RelativeLayout 讓子項目指定它們相對於其他元素的位置(通過ID 來指定)或相對於父布局對象。

本例使用RelativeLayout 顯示三個TextView ,其中第二個TextView(id/view3)佔據整個螢幕中間部分,前面我們已經介紹了如何使用LinearLayout,通過指定View的權重實現同樣的效果Android ApiDemos樣本解析(143):Views->Layouts->LinearLayout->3. Vertical (Padded)

本例使用RelativeLayout ,因為需要通過制定其它View作為參照物,所有子View必須定義id,其它子View通過指定參照物的id來確定其相對位置。

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<!– view1 goes on top –>
<TextView
android:id=”@+id/view1″
android:background=”@drawable/red”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:text=”@string/relative_layout_1_top”/>

<!– view2 goes on the bottom –>
<TextView
android:id=”@+id/view2″
android:background=”@drawable/green”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:text=”@string/relative_layout_1_bottom”/>

<!– view3 stretches betweeen view1 and view2 –>
<TextView
android:id=”@+id/view3″
android:background=”@drawable/yellow”
android:layout_width=”match_parent”
android:layout_height=”0dip”
android:layout_above=”@id/view2″
android:layout_below=”@id/view1″
android:text=”@string/relative_layout_1_center”/>

</RelativeLayout>

TextView1 (id/view1) 指出其相對於父容器RelativeLayout的layout_alignParentTop為True,因此其顯示在螢幕頂部。

TextView2 (id/view2) 指出其相對於父容器RelativeLayout的android:layout_alignParentBottom為True,因此其顯示在螢幕底部。

TextView3(id/view3) 高度設為0,但通過android:layout_above和android:layout_below指出它和TextView1於TextView2之間的相對位置(和兩個TextView上下相鄰),因此TextView3佔據所有剩餘的中間部分。

 

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.