編輯:關於 Android 程式設計
在Android開發實踐中,界面設計往往直接決定了應用的用戶體驗。一個優秀的界面從來不是單靠一個XML佈局文件就能完成的。當某個Activity需要承載大量控件時,若把全部佈局都塞進同一個XML裡,其混亂程度可想而知。本文就結合一個簡單的登陸界面示例,講解如何借助include標籤將佈局合理拆分。以下示例只涉及佈局層面,暫不包含響應邏輯的代碼。
首先是user.xml,用於封裝用戶名輸入區域:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名: " /> <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:id="@+id/userName" android:hint="請輸入用戶名" /> </LinearLayout>
其次是passwd.xml,負責密碼輸入:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼:" /> <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:id="@+id/passWd" android:hint="請輸入密碼" /> </LinearLayout>
login.xml則承載底部按鈕:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:layout_width="80dp" android:layout_height="wrap_content" android:id="@+id/bt" android:hint="確定" /> <Button android:layout_width="80dp" android:layout_height="wrap_content" android:id="@+id/reset" android:layout_toRightOf="@id/bt" android:hint="重置" /> </RelativeLayout>
最後是main.xml,通過include將上述三個片段組裝起來:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_alignParentBottom="true"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_alignParentBottom="true"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/head" android:layout_alignParentTop="true"> <include android:layout_width="fill_parent" layout="@layout/user"> </include> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/middle" android:layout_below="@id/head" android:layout_alignParentLeft="true"> <include android:layout_width="fill_parent" layout="@layout/passwd"> </include> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/foot" android:layout_below="@id/middle" android:layout_alignParentRight="true"> <include android:layout_width="fill_parent" layout="@layout/login"> </include> </LinearLayout> </RelativeLayout> </LinearLayout>
程序運行效果如下:

如果我們把main.xml簡化成下面這種寫法:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_alignParentBottom="true"> <include android:layout_width="fill_parent" layout="@layout/user"> </include> <include android:layout_width="fill_parent" layout="@layout/passwd"> </include> <include android:layout_width="fill_parent" layout="@layout/login"> </include> </LinearLayout>
此時的運行結果變成了:

很明顯,這個結果與預期不符,後面的幾個控件根本沒有正常顯示出來。這究竟是為什麼呢?相信不少人在動手實驗時都遇到過這個坑。
關鍵在於main.xml中缺少對各個被引入佈局的合理組織。每個include所引入的佈局都需要有一個合適的父容器來定位,否則就會出現錯位甚至顯示異常的問題。大家可以嘗試在自己的代碼裡調整相應的佈局結構,親自體會main佈局在整體組織中的重要性。
更多Android相關內容有興趣的讀者可以查看本站專題:《Android視圖View技巧總結》、《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總》、《Android基本組件用法總結》、《Android佈局layout技巧總結》及《Android控件用法總結》。
希望本文能對大家的Android程序設計有所幫助。
[Android] Android Studio 中 Gradle 配置運行淺析
序 既然是「淺析」,自然不會有太深入的內容——沒實際用過的東西我也不會硬寫出來誤導人。本文僅記錄在 Android Studio 中 Gradle 的使用配置。 Grad
安卓日記:自製 RecyclerView 實現仿 QQ 左滑顯示選單
要做出一個類似 QQ 的效果——在列表項上左滑,露出「置頂」與「刪除」兩個選項——核心只需要一個 Scroller,再加上對觸摸事件的細緻處理。 一、自訂 Recycl
Android 使用 AIDL 實作跨進程通訊(IPC)完整教學
前言:在做一件事之前,千萬別先覺得它很難。還沒開始就先在內心產生恐懼,只會阻礙你的進步——往往真的動手做完,才發現沒那麼複雜。 一、AIDL 是什麼?為什麼需要它? A
《Android 源碼設計模式解析與實戰》讀書筆記(二十三):外觀模式 Facade
第二十三章 外觀模式(Facade) 外觀模式是結構型設計模式之一,在實際開發中應用極為廣泛,可以說是我們封裝 API 的常用手法。市面上大多數三方 SDK 都採用了外觀