本篇文章給大家分享的是有關(guān)如何使用RelativeLayout布局,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
恩,先說(shuō)下什么是兄弟組件吧,所謂的兄弟組件就是處于同一層次容器的組件,如圖
圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2并不是兄弟組件,所以組件3不能通過(guò)組件1或2來(lái)進(jìn)行定位,比如layout_toleftof = "組件1"這樣是會(huì)報(bào)錯(cuò)的!切記!關(guān)于這個(gè)兄弟組件定位的最經(jīng)典例子就是"梅花布局"了,下面代碼實(shí)現(xiàn)下:
運(yùn)行效果圖:
實(shí)現(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" > <!-- 這個(gè)是在容器中央的 --> <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>4.margin與padding的區(qū)別
初學(xué)者對(duì)于這兩個(gè)屬性可能會(huì)有一點(diǎn)混淆,這里區(qū)分下:首先margin代表的是偏移,比如marginleft = "5dp"表示組件離容器左邊緣偏移5dp; 而padding代表的則是填充,而填充的對(duì)象針對(duì)的是組件中的元素,比如TextView中的文字比如為TextView設(shè)置paddingleft = "5dp",則是在組件里的元素的左邊填充5dp的空間! margin針對(duì)的是容器中的組件,而padding針對(duì)的是組件中的元素,要區(qū)分開(kāi)來(lái)!下面通過(guò)簡(jiǎn)單的代碼演示兩者的區(qū)別:
比較示例代碼如下:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button"/> <Button android:paddingLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn1"/> <Button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true"/> <Button android:layout_marginLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn2" android:layout_alignParentBottom="true"/> </RelativeLayout>
運(yùn)行效果圖比較:
相信很多朋友都不知道一點(diǎn)吧,平時(shí)我們?cè)O(shè)置margin的時(shí)候都習(xí)慣了是正數(shù)的, 其實(shí)是可以用負(fù)數(shù)的,下面寫(xiě)個(gè)簡(jiǎn)單的程序演示下吧,模擬進(jìn)入軟件后,彈出廣告頁(yè)面的,右上角的cancle按鈕的margin則是使用負(fù)數(shù)的!
效果圖如下:
貼出的廣告Activity的布局代碼吧,當(dāng)然,如果你對(duì)這個(gè)有興趣的話可以下下demo, 因?yàn)閮H僅是實(shí)現(xiàn)效果,所以代碼會(huì)有些粗糙!
<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" tools:context="com.jay.example.relativelayoutdemo.MainActivity" android:background="#00CCCCFF"> <ImageView android:id="@+id/imgBack" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/myicon" /> <ImageView android:id="@+id/imgCancle" android:layout_width="28dp" android:layout_height="28dp" android:layout_alignRight="@id/imgBack" android:layout_alignTop="@id/imgBack" android:background="@drawable/cancel" android:layout_marginTop="-15dp" android:layout_marginRight="-10dp" /> </RelativeLayout>
以上就是如何使用RelativeLayout布局,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:如何使用RelativeLayout布局-創(chuàng)新互聯(lián)
文章地址:http://chinadenli.net/article16/dgjgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、云服務(wù)器、外貿(mào)建站、網(wǎng)站營(yíng)銷、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容