Android學習目錄
- 相對布局
- 相對布局核心屬性
- 案例演示(一)
- 實作步驟
- 案例演示(二)
- 案例演示(三)
相對布局
昨天,我進行了幀形布局的學習,進行我將進行常用布局中的相對布局的學習,
相對布局核心屬性

案例演示(一)
實作步驟
(一)創建安卓應用(RelativeLayoutDemo)


(二)進行activity_main.xml檔案的撰寫
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--中間按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnCenter"
android:layout_centerInParent="true"
android:text="中央"/>
<!--左上按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnUpperLeft"
android:layout_toLeftOf="@id/btnCenter"
android:layout_above="@+id/btnCenter"
android:text="左上"/>
<!--右上按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnUpperRight"
android:layout_toRightOf="@id/btnCenter"
android:layout_above="@+id/btnCenter"
android:text="右上"/>
<!--左下按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLowerLeft"
android:layout_toLeftOf="@+id/btnCenter"
android:layout_below="@+id/btnCenter"
android:text="左下"/>
<!--右下按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLowerRight"
android:layout_toRightOf="@+id/btnCenter"
android:layout_below="@+id/btnCenter"
android:text="右下"/>
<!--確定按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnOK"
android:layout_below="@+id/btnLowerLeft"
android:layout_alignLeft="@+id/btnLowerLeft"
android:layout_marginTop="15dp"
android:text="確定"/>
<!--撤銷按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnCancel"
android:layout_below="@+id/btnLowerRight"
android:layout_alignLeft="@+id/btnLowerRight"
android:layout_marginTop="15dp"
android:text="取消"/>
<!--左上角按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnUpperLeftCorner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="左上角"/>
<!--右上角按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnUpperRightCorner"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="右上角"/>
<!--左下角按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLowerLeftCorner"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="左下角"/>
<!--右下角按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLowerRightCorner"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="右下角"/>
</RelativeLayout>
代碼所示的按鈕布局,如圖所示:

運行代碼,所得結果如下圖所示:

為實作如下圖所示的按鈕分布圖,我們可以在上面所述案例的代碼中添加如下所示代碼即可,

<!--頂按鈕-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnUpperCenter"
android:layout_above="@+id/btnUpperLeft"
android:layout_centerInParent="true"
android:text="頂"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnLowerCenter"
android:layout_below="@+id/btnOK"
android:layout_centerInParent="true"
android:text="底"/>
案例演示(二)
效果如下圖所示:

詳細代碼如圖所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edtName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="請輸入姓名:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:layout_alignBaseline="@+id/edtName"
android:layout_toLeftOf="@+id/edtName"
android:text="姓名"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnCancel"
android:layout_alignParentRight="true"
android:layout_below="@id/edtName"
android:text="取消"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnOK"
android:layout_alignBaseline="@id/btnCancel"
android:layout_toLeftOf="@+id/btnCancel"
android:text="確定"/>
</RelativeLayout>
案例演示(三)
效果如圖所示:

詳細代碼如下(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context=".MainActivity">
<!--輸入框-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:text="注冊個人資訊"
android:padding="15dp"
android:textSize="30dp"
android:id="@+id/Tilte"
android:textColor="#0000ff"/>
<EditText
android:id="@+id/edtName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Tilte"
android:layout_centerInParent="true"
android:text="請輸入姓名:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:layout_alignBaseline="@+id/edtName"
android:layout_toLeftOf="@+id/edtName"
android:textSize="20dp"
android:textColor="@color/black"
android:text="用戶名:"/>
<EditText
android:id="@+id/edtPassword"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@+id/edtName"
android:layout_centerInParent="true"
android:text="請輸入密碼:" />
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:paddingTop="17dp"
android:layout_toLeftOf="@+id/edtPassword"
android:text="密碼:"
android:textColor="@color/black"
android:textSize="20dp" />
<EditText
android:id="@+id/edtNumber"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@+id/edtPassword"
android:layout_centerInParent="true"
android:text="請輸入電話號碼:" />
<TextView
android:id="@+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvPassword"
android:paddingTop="17dp"
android:layout_toLeftOf="@+id/edtNumber"
android:text="電話:"
android:textColor="@color/black"
android:textSize="20dp" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@+id/edtNumber"
android:layout_centerInParent="true"
android:text="請輸入郵箱地址:" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvNumber"
android:paddingTop="17dp"
android:layout_toLeftOf="@+id/edtEmail"
android:text="郵箱:"
android:textColor="@color/black"
android:textSize="20dp" />
<!--單選框-->
<!--單選按鈕-->
<LinearLayout
android:layout_width="380dp"
android:id="@+id/tvChoose01"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_below="@+id/edtEmail"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvGender"
android:textColor="@color/black"
android:text="@string/gender1"
android:textSize="20dp"/>
<RadioGroup
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/rgGender"
android:orientation="horizontal">
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/rbMale"
android:checked="true"
android:text="@string/gender1_1"/>
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/rbFemale"
android:layout_marginLeft="15dp"
android:text="@string/gender1_2"/>
</RadioGroup>
</LinearLayout>
<!--復選框-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvChoose02"
android:layout_below="@+id/tvChoose01">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvHobby"
android:text="@string/hobby"
android:textColor="#000000"
android:textSize="16dp"/>
<CheckBox
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/cbMusic"
android:text="@string/music" />
<CheckBox
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/cbTravel"
android:text="@string/travel"/>
<CheckBox
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/cdFood"
android:text="@string/food"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvChoose02"
android:padding="12dp">
<Button
android:layout_width="40dp"
android:layout_height="wrap_content"
android:id="@+id/btnOK"
android:layout_weight="1"
android:text="提交"
/>
<Button
android:layout_width="40dp"
android:layout_height="wrap_content"
android:id="@+id/btnClear"
android:layout_weight="1"
android:text="清楚"
android:layout_marginLeft="10dp" />
</LinearLayout>
</RelativeLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/248124.html
標籤:其他
上一篇:android學習日志
下一篇:安卓開發學習——day3
