編輯:關於 Android 程式設計
Relative(相對佈局)顧名思義,是讓容器內的子元件依據彼此之間、或與容器之間的相對關係來定位的佈局方式。掌握它,能讓畫面的排列既靈活又精準。
用於元件之間相對定位的屬性:
android:layout_above 置於給定 ID 的元件之上 android:layout_below 置於給定 ID 的元件之下 android:layout_toLeftOf 置於給定 ID 的元件之左 android:layout_toRightOf 置於給定 ID 的元件之右
相對於父容器的定位屬性:
android:layout_alignParentLeft 位於父容器左側 android:layout_alignParentRight 位於父容器右側 android:layout_alignParentTop 位於父容器頂部 android:layout_alignParentBottom 位於父容器底部
對齊相關屬性:
android:layout_alignBaseline 基線對齊給定 ID 的基線 android:layout_alignBottom 與給定 ID 底部對齊 android:layout_alignLeft 與給定 ID 左側對齊 android:layout_alignRight 與給定 ID 右側對齊 android:layout_alignTop 與給定 ID 頂部對齊 android:layout_centerHorizontal 置於水平中央 android:layout_centerInParent 置於父容器的水平與垂直中央 android:layout_centerVertical 置於垂直中央
特別提醒:不能在 RelativeLayout 容器本身與其子元件之間產生循環依賴。例如,當容器高度設為 WRAP_CONTENT 時,就不可再將子元件高度設為 ALIGN_PARENT_BOTTOM。
以下是一個以純 RelativeLayout 完成的登入畫面:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10px"
android:background="#00aa00">
<TextView
android:id="@+id/tv_heard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="12pt"
android:text="XXX系統登錄界面" />
<TextView android:id="@+id/tv_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_heard"
android:layout_marginTop="30px"
android:text="用戶名:" />
<EditText android:id="@+id/txt_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_heard"
android:layout_toRightOf="@id/tv_account"
android:layout_alignBaseline="@id/tv_account" />
<TextView android:id="@+id/tv_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_account"
android:layout_marginTop="30px"
android:text="密 碼:" />
<EditText android:id="@+id/txt_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_account"
android:layout_toRightOf="@id/tv_pwd"
android:layout_alignBaseline="@id/tv_pwd" />
<Button android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_pwd"
android:layout_alignParentRight="true"
android:layout_marginTop="30px"
android:text="退出" />
<Button android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_pwd"
android:layout_toLeftOf="@id/btn_exit"
android:layout_marginTop="30px"
android:layout_marginRight="50px"
android:text="登錄" />
</RelativeLayout>
將 RelativeLayout 與 LinearLayout 結合,往往能更快、更直觀地完成排版:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aaff"
android:padding="10px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="12pt"
android:text="XXX系統登錄界面" />
<LinearLayout android:id="@+id/linear_one"
android:layout_below="@id/tv_heard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/linear_second"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_below="@id/linear_one">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/linear_third"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_below="@id/linear_second"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30px"
android:text="退出" />
</LinearLayout>
</RelativeLayout>
透過這種「外層相對定位、內層線性排列」的組合策略,你能兼得兩種佈局的優勢,讓複雜畫面的實作事半功倍。
Android Studio 效率技巧:一個快捷鍵快速提取方法
開發時經常遇到這種情況:一個方法裡堆了幾十行代碼,想把其中一段邏輯剝離出去單獨封裝。多數人的做法是先選中代碼,再 Ctrl + X 剪下來,然後手動敲方法簽名粘回去——步
QQ火炬傳遞總是失敗 原因與解法整理
2016里約奧運會即將開幕,QQ也推出了一個有趣的AR火炬傳遞活動,讓大家都能體驗點亮火炬的樂趣,參與度相當高。不過也有人反映傳遞過程中總是失敗,那該怎麼辦?小編下面就來
Android 多選方塊對話框用法解析
多選方塊對話框概念 多選方塊對話框可讓使用者同時選擇多個項目,常用於興趣、通知設定或多條件篩選。 建立多選對話框 @Override protected Dialog o
Android SQLite 資料庫入門:從建表到增刪改查的完整流程
App 的核心價值往往取決於怎麼處理資料,而 Android 內建的 SQLite 正是最輕量、最直接的選擇——不需另外安裝服務,整個資料庫就是一個檔案。本文從「把結構定