相對布局(重點):相對布局是通過相對定位的方式讓控件出現在布局任意位置;
在相對布局中如果不指定控件擺放的位置,那么控件都會被默認放在RelativeLayout的左上角。因此要先指定第一個控件的位置,再根據一個控件去給其他控件布局。
圖中的組件1,2就是兄弟組件了,而組件3與組件1或組件2並不是兄弟組件,所以組件3不能通過 組件1或2來進行定位
比如layout_toleftof = "組件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>
margin與padding的區別
初學者對於這兩個屬性可能會有一點混淆,這里區分下: 首先margin代表的是偏移,比如marginleft = "5dp"表示組件離容器左邊緣偏移5dp; 而padding代表的則是填充,而填充的對象針對的是組件中的元素,比如TextView中的文字 比如為TextView設置paddingleft = "5dp",則是在組件里的元素的左邊填充5dp的空間! margin針對的是容器中的組件,而padding針對的是組件中的元素,要區分開來! 下面通過簡單的代碼演示兩者的區別:
比較示例代碼如下:
<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>
小tip:margin可以設置為負數
平時我們設置margin的時候都習慣了是正數的, 其實是可以用負數的,下面寫個簡單的程序演示下吧,模擬進入軟件后,彈出廣告 頁面的,右上角的cancle按鈕的margin則是使用負數的!