編輯:關於 Android 程式設計
寫 XML 佈局時,你一定見過 layout_width 這類以 layout_ 開頭的屬性。它們並不是 View 自身的屬性,而是父容器用來安排子元素的「佈局參數」。搞懂這層關係,再回頭看 LinearLayout 與 RelativeLayout 的種種屬性,思路就清晰多了。
這些屬性的作用是讓子 View 適配它的父 ViewGroup。每個 ViewGroup 類別都實作了一個繼承自 ViewGroup.LayoutParams 的內部類,裡面定義了該容器特有的、用來描述子 View 尺寸與位置的屬性。
換句話說:父容器決定你能寫哪些 layout 屬性,而你的設定又會繼續影響你自己的子元素。下面這段 XML 就是很好的說明:
<!-- 根佈局是 LinearLayout -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0f0"
android:orientation="vertical">
<!-- LinearLayout 的子 View 是 RelativeLayout -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00">
<TextView
android:id="@+id/textView1InRL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:text="TextView" />
<TextView
android:id="@+id/textView2InRL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:text="TextView2"
android:layout_toRightOf="@id/textView1InRL" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:text="TextView3"
android:layout_below="@id/textView2InRL" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1InLinearLayout" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2InLinearLayout" />
</LinearLayout>
這裡 RelativeLayout 的 layout_width、layout_height 由父容器 LinearLayout 解讀;而它自己的 layout_toRightOf、layout_below 則是給三個 TextView 用的。同一個 View,站在不同位置就有不同身份。
把子元素沿單一方向排列,可以垂直也可以水平。當內容總長度超出螢幕時,就需要捲軸來輔助。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 這裡放要捲動的內容 -->
</LinearLayout>
</ScrollView>
</LinearLayout>
要留意 ScrollView 只能有一個直接子元素,所以通常都會再包一層 LinearLayout。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="帳號:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼:" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="登入" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="註冊" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="500dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinearLayout 大小超過螢幕的測試" />
</LinearLayout>
</ScrollView>
</LinearLayout>
這個例子刻意把 ImageView 的高度設成 500dp,讓總高度超過螢幕,用來驗證捲軸是否生效。注意 LinearLayout 可以嵌套,內層改成 horizontal 就能做出「兩個按鈕並排」的效果。
顧名思義,它靠「相對關係」定位。你可以讓子元素彼此對齊(例如按鈕 A 在文字 B 下方),也可以讓子元素相對父容器定位(水平居中、靠右上等)。
若完全不設定任何規則,所有子 View 都會預設堆在左上角。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- layout_centerHorizontal 讓標題水平居中 -->
<TextView
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:text="登入介面" />
<!-- layout_below 讓輸入框排在標題下方,layout_marginTop 留出 24dp 間距 -->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:hint="username"
android:ems="10">
<requestFocus />
</EditText>
<!-- editText2 緊接在 editText1 下方 -->
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword" />
<!-- layout_alignRight 讓取消鈕與 editText2 右緣對齊 -->
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText2"
android:layout_alignRight="@id/editText2"
android:text="取消" />
<!-- layout_toLeftOf 讓確定鈕排在取消鈕左側 -->
<Button
android:id="@+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText2"
android:layout_toLeftOf="@id/cancelButton"
android:text="確定" />
</RelativeLayout>
幾個實用提醒:@+id/ 代表建立新 id,@id/ 代表引用既有 id,兩者混用是 RelativeLayout 常見的錯誤來源。另外 RelativeLayout 的依賴關係必須是「被引用者先宣告」,否則會找不到資源,這一點在複雜佈局裡特別容易踩到。
Android 九宮格手勢密碼的設計與實作
九宮格解鎖是常見的交互形式,本質上就是一個自定義 View:畫九個圓、判斷手指壓在哪個圓上、把路徑連起來,最後把序列輸出成密碼。 一、效果與功能 1. 程序啟動後展示 3
Android 中監聽 ScrollView 滑動事件的實現
有時候我們需要實時掌握 ScrollView 的滑動情況,例如滑動了多少距離、是否已經滑到頂部或底部。遺憾的是 SDK 並沒有直接提供對外的方法,只保留了一個 prote
SwipeListView 實作:打造類 QQ 的滑動側邊操作列表
左右滑動列表項、露出背後的操作按鈕,這種互動從 QQ 之後幾乎成了清單的標準語彙。自己從 ListView 的 onTouch 刻一套並不划算,社群已有成熟方案:47de
詳解 Android MVP 開發模式
本文主要講解 MVP 開發模式,並通過一個用戶登錄的具體實例演示其落地方式。 一、簡介 MVP(Model View Presenter)是經典 MVC(Model Vi