日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android之布局RelativeLayout

發(fā)布時間:2025/7/14 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之布局RelativeLayout 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

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

1.容器定位

父容器定位屬性示意:

兄弟容器定位屬性示意:

?

舉例:梅花布局

實現(xiàn)代碼如下:

<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的區(qū)別

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

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

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

轉(zhuǎn)載于:https://www.cnblogs.com/albertarmstrong/p/9221400.html

總結(jié)

以上是生活随笔為你收集整理的Android之布局RelativeLayout的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。