編輯:關於 Android 程式設計
Android 應用通常由多個 Activity 組成,使用者看到的每一個畫面,本質上都是一棵由控制項構成的視圖樹。而這些控制項如何排列、佔據多少空間、彼此如何對齊,全部由布局管理器決定。可以這樣理解:布局是建築的框架,控制項依框架規則就位,最後才拼湊出看得見的漂亮介面。
Android 中所有可視化控制項都繼承自 android.view.View。設定視圖有兩條路:一是用 XML 描述屬性,由系統在啟動時依設定檔動態建立;二是直接在 Java 代碼中 new 出對應物件。前者結構清楚、易於預覽,是絕大多數情況的首選;後者則適合需要動態生成列表項的場景。
每個專案的 res/layout 目錄就是存放佈局檔的地方,檔名通常與對應的 Activity 同名,副檔名為 .xml。建立控制項時需指定 id,寫成 android:id="@+id/名字",系統會自動在 gen 目錄產生對應的 R 資源常數。
Activity 通常在 onCreate() 中呼叫 setContentView() 載入佈局,再用 findViewById() 依 id 取得控制項實例。
private Button btnSndMag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 載入 main.xml
btnSndMag = (Button) this.findViewById(R.id.btnSndMag); // 依 id 取得 Button
// ... 後續設定監聽器等
}
Android 原生提供五種主要布局:LinearLayout(線性)、FrameLayout(單幀)、AbsoluteLayout(絕對)、TableLayout(表格)、RelativeLayout(相對)。其中最常用的是前三個裡的線性、表格與相對布局,而且它們全部可以任意嵌套。
線性布局把子元素(可以是控制項,也可以是另一個布局)依水平或垂直方向依次排列,每個元素都緊接在上一個之後。方向由 android:orientation 決定,取值為 vertical 或 horizontal。
android:layout_weight 用來表示子元素佔據剩餘空間的比例。這裡有個流傳很廣的誤解:許多人以為 weight 越大佔的空間越小,實際上恰恰相反——數值越大佔據的空間越多。會產生困惑是因為當 layout_width 設為 match_parent 時,各子元素先各自佔滿父容器,再由 weight 按比例「壓縮」,視覺結果看起來就像反比。實務上的安全寫法是:使用 weight 分欄時,把該方向的尺寸設為 0dp,讓比例計算回歸直覺。
所有子元素都疊在左上角同一位置,後加入的會蓋在前面之上。適合需要層疊效果的場合,例如相片上加浮水印、地圖上疊加浮動按鈕、或 ProgressBar 覆蓋在 ImageView 上。它也是 TabHost 與 Fragment 容器的底層實現。
以 android:layout_x 與 android:layout_y 指定精確座標。看似精準,實則在多元尺寸的手機生態中幾乎必然錯位,官方早已標記為不建議使用。僅在需要完全固定的情境(如自製遊戲畫布)才會考慮。
以列與欄組織子元素,每一行由 TableRow 定義,行內控制項自動依序填欄。適合需要對齊的表單類介面。欄寬可用 android:stretchColumns 指定哪些欄要撐滿剩餘寬度,android:shrinkColumns 則用於內容過長時壓縮。
允許子元素相對於父容器或彼此定位,例如 layout_centerInParent、layout_above、layout_toRightOf、layout_alignLeft 等。優點是層級扁平、效能好,能用一個 RelativeLayout 取代好幾層嵌套的 LinearLayout;缺點是屬性繁多,複雜畫面的宣告順序需要仔細安排,因為後面元素的定位常依賴前面元素的 id。
下面用嵌套的 LinearLayout 拼一個計算器鍵盤,可以實際感受線性布局的組合能力:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".MainActivity">
<!-- 第一行:顯示標籤,水平布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/msg"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="">
</EditText>
</LinearLayout>
<!-- 第二行:mc m+ m- mr -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="mc" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="m+" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="m-" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="mr" android:layout_weight="1"></Button>
</LinearLayout>
<!-- 第三行:C +/- / * -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="C"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="+/-"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="/"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="*"></Button>
</LinearLayout>
<!-- 第四行:7 8 9 - -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="7" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="8" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="9" android:layout_weight="1"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="-" android:layout_weight="1"></Button>
</LinearLayout>
<!-- 第五行:4 5 6 + -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="4"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="5"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="6"></Button>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:text="+"></Button>
</LinearLayout>
<!-- 最外層為水平布局:左側是 1 2 3 / 0 . 的垂直組合,右側是 = -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 1 2 3 與 0 . 構成垂直布局 -->
<LinearLayout
android:orientation="vertical"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="1"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="2"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="3"></Button>
</LinearLayout>
<!-- 注意這裡把寬度設為 0px 再配 weight,比例才符合直覺 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="2" android:text="0"></Button>
<Button android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1" android:text="."></Button>
</LinearLayout>
</LinearLayout>
<!-- 單一 Button 構成的垂直布局 -->
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button android:layout_width="match_parent" android:layout_height="match_parent"
android:text=">"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
優先保持扁平。布局層級越深,量測與配置階段的成本越高。能用一層 RelativeLayout 或 ConstraintLayout 解決的,就不要疊三層 LinearLayout。
換算成 ConstraintLayout 更划算。如今新專案幾乎都以 ConstraintLayout 為預設根布局,它用相對約束取代嵌套,能在完全扁平的結構下完成上面這種計算器介面,效能明顯更好。上面範例的價值在於理解 weight 與嵌套的運作原理,而非建議照抄。
單位用 dp,文字用 sp。前者隨屏幕密度縮放,後者還會跟著使用者設定的字級變動,這是無障礙設計的基礎要求。
Android 開發資源大全:開源專案、優質部落格與站點導引
這就是開源最大的好處。透過 GitHub、各大論壇與技術部落格,你總能找到對自己有用的東西。對做技術的人來說,鑽深一門功夫當然重要,但適度開闊視野、看看別人已經做出什麼成
Android MVP 架構學習實例:從 MVC 到 MVP 的實踐
為什麼需要探討架構隨著行動開發的不斷演進,專案設計模式也越來越講究。良好的架構不僅能加快開發速度,也能讓後續維護與擴充變得更輕鬆。不過,並沒有一種萬能架構適合所有團隊與所
微信怎麼定時開啟消息勿擾模式
有時候只想安靜休息,微信卻響個不停——尤其是一些群聊,消息一條接一條地刷,自己又沒空去看。這種情況下,把勿擾模式用起來會舒服很多。 微信怎麼定時設置消息勿打擾? 1. 打
跟我學 Android 之十一:清單與適配器
本章將介紹清單類元件的使用,內容包含:清單視圖的概念、ListView 列表視圖,以及 Spinner 下拉視圖。學習目標是理解 MVC 設計思想、掌握 AdapterV