標籤:
Android布局分為五大類:相對布局、線性布局、表格版面配置、幀布局、網格布局
相對布局
文法格式:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmls:tools=""http://schemas.android.com/tools
android:layout_width=" "
android:layout_height=" ">
<Widgets>
android:id="@+id/ "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal=" "
android:layout_alignParentLeft=" "
android:layout_marginLeft=" "
</Widgets>
</RelativeLayout>
相對父容器布局的文法屬性
屬性名稱 屬性說明
layout_alignParentLeft 以父容器的左邊緣為參照標準
layout_marginLeft 控制項左邊緣距離父容器左邊緣的距離
layout_alignParentRigth 以父容器的有邊緣為參照標準
layout_marginRight 控制項右邊緣距離父容器右邊緣的距離
layout_alignParentTop 以父容器的上邊緣為參照標準
layout_marginTop 控制項上邊緣距離父容器上邊緣的距離
layout_alignParentBottom 以父容器下邊緣為參照標準
layout_marginBottom 控制項下邊緣距離父容器下邊緣的距離
layout_centerHorizontal 控制項在父容器中水平置中
layout_centerHorizontal 控制項在父容器中垂直置中
layout_centerInParent 控制項在父容器中央
例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="128dp"
android:layout_centerHorizontal="true"
android:text="button1" />
</RelativeLayout>
相對控制項布局的文法屬性
屬性名稱 屬性說明
layout_alignLeft 以已知控制項的左邊緣為參照標準
layout_marginLeft 控制項的左邊緣與已知控制項之間的距離
layout_alignRight 以已知控制項的右邊緣為參照標準
layout_marginRight 控制項的右邊緣與已知控制項之間的距離
layout_alignTop 以已知控制項的上邊緣為參考標準
layout_marginTop 控制項的上邊緣與已知控制項之間的距離
layout_alignBottom 以已知控制項的下邊緣為參考標準
layout_marginBottom 控制項的下邊緣與已知控制項之間的距離
layout_alignBaseLine 以已知控制項的BaseLine為參考標準
layout_toRightOf 控制項位於已知控制項的右側
layout_toLeftOf 控制項位於已知控制項的左側
layout_above 控制項位於已知控制項的上側
layout_below 控制項位於已知控制項的下側
例子
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn1"
android:layout_marginBottom="85dp"
android:layout_toRightOf="@+id/btn1"
android:text="button2"/>
</RelativeLayout>
Android布局---相對布局