Android之布局RelativeLayout

來源:互聯網
上載者:User

標籤:技術分享   nbsp   實現   --   drawable   資源   分配   方便   relative   

 

  線性布局的weight屬性在等比例分配時比較方便,但是對複雜的介面,嵌套多層LinearLayout布局會導致渲染變慢,佔用更多系統資源;而使用RelativeLayout的話,可能僅僅需要一層就可以完成了,以父容器或者兄弟組件參考+margin +padding就可以設定組件的顯示位置。

1.容器定位

父容器定位屬性示意:

兄弟容器定位屬性示意:

 

舉例:梅花布局

實現代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:id="@+id/RelativeLayout1"        android:layout_width="match_parent"        android:layout_height="match_parent" >            <!-- 這個是在容器中央的 -->        <ImageView            android:id="@+id/img1"             android:layout_width="80dp"            android:layout_height="80dp"            android:layout_centerInParent="true"            android:src="@drawable/pic1"/>                <!-- 在中間圖片的左邊 -->        <ImageView            android:id="@+id/img2"             android:layout_width="80dp"            android:layout_height="80dp"            android:layout_toLeftOf="@id/img1"            android:layout_centerVertical="true"            android:src="@drawable/pic2"/>                <!-- 在中間圖片的右邊 -->        <ImageView            android:id="@+id/img3"             android:layout_width="80dp"            android:layout_height="80dp"            android:layout_toRightOf="@id/img1"            android:layout_centerVertical="true"            android:src="@drawable/pic3"/>                <!-- 在中間圖片的上面-->        <ImageView            android:id="@+id/img4"             android:layout_width="80dp"            android:layout_height="80dp"            android:layout_above="@id/img1"            android:layout_centerHorizontal="true"            android:src="@drawable/pic4"/>                <!-- 在中間圖片的下面 -->        <ImageView            android:id="@+id/img5"             android:layout_width="80dp"            android:layout_height="80dp"            android:layout_below="@id/img1"            android:layout_centerHorizontal="true"            android:src="@drawable/pic5"/>        </RelativeLayout>

 

2. margin與padding的區別

 margin表示組件到容器邊緣距離,如:marginleft = "5dp" 表示組件距離容器左邊緣5dp

 padding代表的則是填充,例如:TextView設定paddingleft = "5dp",則是在組件裡的元素的左邊填充5dp的空間。

  margin針對的是容器中的組件,而padding針對的是組件中的元素

Android之布局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.