Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android include標籤用法詳解:優雅拆分佈局文件

編輯:關於 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程序設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved