相對佈局RelativeLayout的子控件屬性和操作

RelativeLayout是相對佈局控件,它包含的子控件將以控件之間的相對位置或者子類控件相對父類容器的位置方式排列。

子類控件常用屬性

android:layout_alignParentLeft
-子類控件相對當前父類容器靠左

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_alignParentLeft="true"/>

顯示效果:

這裏寫圖片描述

android:layout_alignParentTop
-子類控件相對當前父類容器靠上

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_alignParentTop="true"/>

顯示效果:

這裏寫圖片描述

android:layout_marginLeft
-子類控件距離父類容器左邊的距離

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_marginLeft="40dp"/>

顯示效果:

這裏寫圖片描述
android:layout_marginTop

-子類控件距離父類容器上邊的距離

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_marginTop="40dp"/>

顯示效果:

這裏寫圖片描述

android:layout_centerInParent
-子類控件相對於父類容器居中

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_centerInParent="true"/>

顯示效果:

這裏寫圖片描述

android:layout_centerHorizontal
-子類控件相對於父類容器水平居中

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_centerHorizontal="true"/>

顯示效果:

這裏寫圖片描述

android:layout_centerVertical
-子類控件相對於父類容器垂直居中

代碼:

<Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        android:layout_centerVertical="true"/>

顯示效果:

這裏寫圖片描述

android:layout_below
-該控件位於給id控件的下側

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"/>
    <Button 
        android:id="@+id/bt2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Two"
        android:layout_below="@+id/bt1"/>

顯示效果:

這裏寫圖片描述

android:layout_roRightOf
-該控件位於給定id控件的右側

代碼:

    <Button 
        android:id="@+id/bt1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"/>
    <Button 
        android:id="@+id/bt2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Two"
        android:layout_toRightOf="@+id/bt1"/>

顯示效果:

這裏寫圖片描述

android:layout_above
-該控件位於給定id控件的上側

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bt1"
        android:text="Two" />

顯示效果:

這裏寫圖片描述

android:layout_toLeftOf
-該控件位於給定id控件的左側

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/bt1"
        android:text="Two" />

顯示效果:

這裏寫圖片描述

android:layout_alignBaseline
-該控件內容與給定id控件的內容在一條線上

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_alignBaseline="@+id/bt1"
        android:text="Two" />

顯示效果:

這裏寫圖片描述

android:layout_alignBottom
-該控件的底部邊緣與給定id控件的底部邊緣對齊

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_alignBottom="@+id/bt1"
        android:text="Two" />

顯示效果:

這裏寫圖片描述
android:layout_alignRight

-該控件的底部邊緣與給定id控件的右側邊緣對齊

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_alignRight="@+id/bt1"
        android:text="Two" />

android:layout_alignLeft
-該控件的底部邊緣與給定id控件的左側邊緣對齊

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_alignLeft="@+id/bt1"
        android:text="Two" />

android:layout_alignTop
-該控件的底部邊緣與給定id控件的頂部邊緣對齊

代碼:

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_alignTop="@+id/bt1"
        android:text="Two" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章