RelativeLayout屬性和使用, 實現上面view疊加在下面view之上的效果

Android RelativeLayout 屬性

// 相對於給定ID控件

Android:layout_above 將該控件的底部置於給定ID的控件之上;

android:layout_below 將該控件的底部置於給定ID的控件之下;

android:layout_toLeftOf    將該控件的右邊緣與給定ID的控件左邊緣對齊;

android:layout_toRightOf  將該控件的左邊緣與給定ID的控件右邊緣對齊;

android:layout_alignBaseline  將該控件的baseline與給定ID的baseline對齊;

android:layout_alignTop        將該控件的頂部邊緣與給定ID的頂部邊緣對齊;

android:layout_alignBottom   將該控件的底部邊緣與給定ID的底部邊緣對齊;

android:layout_alignLeft        將該控件的左邊緣與給定ID的左邊緣對齊;

android:layout_alignRight      將該控件的右邊緣與給定ID的右邊緣對齊;

// 相對於父組件

android:layout_alignParentTop      如果爲true,將該控件的頂部與其父控件的頂部對齊;

android:layout_alignParentBottom 如果爲true,將該控件的底部與其父控件的底部對齊;

android:layout_alignParentLeft      如果爲true,將該控件的左部與其父控件的左部對齊;

android:layout_alignParentRight    如果爲true,將該控件的右部與其父控件的右部對齊;

// 居中

android:layout_centerHorizontal 如果爲true,將該控件的置於水平居中;

android:layout_centerVertical     如果爲true,將該控件的置於垂直居中;

android:layout_centerInParent   如果爲true,將該控件的置於父控件的中央;

// 指定移動像素

android:layout_marginTop      上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight   右偏移的值;

 

example:

        1.

 

android:layout_below = "@id/***"

android:layout_alignBaseline = "@id/***"

android:layout_alignParentTop = true

android:layout_marginLeft = “10px”

2. 使用RelativeLayout實現疊加的效果上面的view覆蓋下面的view。

對任何佈局,通過android:layout_marginXXX等屬性設置爲負值來實現相鄰view之間的疊加效果,

例如:

android:layout_marginTop="-50dip" 可實現與相鄰的頂端view疊加50dip區域的效果。

但是,誰疊加在誰的上面是由他們在xml文件中的描述順序決定的, 後出現的會在上面。
因此如果要讓佈局上面的view覆蓋在下面的view之上,不能使用LinearLayout。因爲, 對於LinearLayout,在xml文件中的描述view時,必須先描述上方的view,讓後描述下方的view,所以下方view的會覆蓋在上方view之上。
可以使用RelativeLayout。因爲在Relativelayout的佈局中,我們可以先描述處於下方的view,後描述上方的view。(注意, 此時先描述的View需要使用後描述的view的ID, 需要使用"@+id/相應的viewID")
例如:下面就是藍色的button會覆蓋綠色button之上的例子

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

>

   <Button android:layout_below="@+id/bt1"

   android:layout_above="@+id/bt2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ff00ff00"

android:layout_marginTop="-50dip"

/>

<Button android:id="@+id/bt1"

android:layout_width="fill_parent"

android:layout_height="100dip"

android:background="#ff0000ff"

/>

<Button android:id="@+id/bt2"

android:layout_alignParentBottom="true"

android:layout_width="fill_parent"

android:layout_height="100dip"

android:background="#ff0000ff"

/>

</RelativeLayout>

發佈了27 篇原創文章 · 獲贊 52 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章