編輯:關於 Android 程式設計
再花俏的介面,拆到底層也不過是這四種佈局的組合與巢狀。搞懂各自的定位規則,排版就不再是試錯。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="垂直1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="垂直2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="水平上對齊" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="水平垂直居中" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="水平下對齊" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="請輸入..."/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="提交" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請輸入..."/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
</LinearLayout>
orientation:horizontal 或 vertical,決定排列方向。
layout_gravity:元素相對於父容器的對齊方式,可為 top、bottom、center、left、right 等,多值以 | 串接,例如右上對齊寫成 top|right。
layout_weight:佔用剩餘空間的比例。若 A 設 1、B 設 3,兩者分別佔四分之一與四分之三,此時寬度不該再由 layout_width 決定,規範寫法是搭配 0dp。若只有 A 設了 weight、B 沒設且寬度為 wrap_content,則 B 依自身內容佔寬,剩下的空間全給 A。
<LinearLayout ...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" android:text="我在左下"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:text="我在中間"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:text="我在右上"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" android:text="參照按鈕"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_above="@id/button_2"
android:layout_toRightOf="@id/button_2" android:text="我在右上"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/button_2"
android:layout_toLeftOf="@id/button_2" android:text="我在左下"/>
</RelativeLayout>
</LinearLayout>
屬性值為 true/false 的對齊類:
layout_centerHorizontal、layout_centerVertical(水平/垂直居中)、layout_centerInParent(相對父元素雙軸居中)、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight、layout_alignParentTop。
屬性值為控件 id 的相對定位類:
layout_above、layout_below(在某元素上/下方)、layout_toLeftOf、layout_toRightOf(在某元素左/右方)、layout_alignTop、layout_alignBottom、layout_alignLeft、layout_alignRight(與某元素邊界對齊)。
使用 RelativeLayout 時務必注意:被參照的控件必須先出現在版面檔中,且參照用 @id/ 而非 @+id/,否則會產生非預期的效果。層數越深效能越差,能扁平化就別巢狀。
所有子元素都從左上角開始繪製,後者疊在前者之上,常用於圖片與文字疊放、或 Fragment 容器:
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一個按鈕"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一個輸入框"/>
</FrameLayout>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_height="wrap_content" android:text="郵箱"/>
<EditText android:layout_height="wrap_content"
android:inputType="textEmailAddress" android:hint="請輸入您的郵箱" />
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content" android:text="密碼"/>
<EditText android:layout_height="wrap_content"
android:inputType="textPassword" android:hint="請輸入密碼" />
</TableRow>
<TableRow>
<Button android:layout_height="wrap_content"
android:layout_span="2" android:text="注冊" />
</TableRow>
</TableLayout>
需要讓某一欄自動填滿剩餘寬度時,加上 stretchColumns:
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
...
</TableLayout>
TableRow 代表一行,row 內每個子元素即為一格。
layout_span 用於合併儲存格,設為 2 表示佔兩欄。
stretchColumns 指定要拉伸的欄索引(從 0 起算),設為 1 即讓第二欄吃滿剩餘寬度。
佈局本身可以嵌套佈局。當某段畫面(例如標題欄)獨立性高又重複使用,或版面複雜到難以維護時,把它獨立成一個 XML 再用 include 引入會清楚很多。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/example"/>
</LinearLayout>
選型上可以記一個原則:單純直排或橫排用 LinearLayout 搭配 weight;需要跨列對齊用 RelativeLayout 或 ConstraintLayout;只是疊放圖文用 FrameLayout;表單類以列為單位對齊用 TableLayout。若是新專案,ConstraintLayout 幾乎可以取代前三種,並把巢狀層數壓到最淺,效能表現明顯更好。
Picasso 圖片下載與快取庫使用指南
Picasso 是什麼 Picasso 是 Square 開源的 Android 圖片載入與快取庫,主要用來簡化圖片從網路或本地資源載入到 ImageView 的流程。它
漫談 Android 系統(七):Log 系統的源碼追蹤(上)
前言 羅升陽的《Android 系統源代碼情景分析》裡講到過 log 最終是怎麼顯示出來的,但書上的描述畢竟停留在概念層面——代碼裡到底是如何落地的?這個問題值得細細追一
Android AsyncTask 異步任務執行機制完整說明
AsyncTask 的核心概念 AsyncTask 是 Android 早期常用的異步任務工具,用來在背景執行耗時工作,並將結果回傳給 UI 執行緒。它讓開發者能以較直觀
Android自定義安全輸入法:防止密碼被第三方竊取
本文為大家分享一個自定義輸入法的實現示例,供感興趣的讀者參考。 對於Android用戶來說,大多數人都習慣使用第三方輸入法。然而在輸入密碼(尤其是支付相關的密碼)時,第三