編輯:Android 開發實例
上一篇介紹了版面配置優化的工具——層級觀察器 Hierarchy Viewer 與版面配置分析工具 layoutopt。若看過上一篇會注意到,layoutopt 曾建議將 <FrameLayout/> 換成 <merge/>。事實上,<merge/> 標籤在 UI 結構優化中扮演著非常重要的角色,透過它能刪減多餘的層級,達到優化 UI 的目的。
先來看看上一篇中使用的框架配置所對應的樹狀結構圖。根節點與 LinearLayout 上方那棵子樹為 Android 的視窗配置,後方的 TextView 即為標籤。可以發現,被框出的 FrameLayout 是另一個 FrameLayout 的唯一子元素,且該父節點並沒有額外屬性,也就是說它完全是冗餘的,這時就該用 <merge/> 標籤來解決。改寫後的配置如下:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="300dip" android:layout_height="300dip" android:background="#00008B" android:layout_gravity="center" /> <TextView android:layout_width="250dip" android:layout_height="250dip" android:background="#0000CD" android:layout_gravity="center" /> <TextView android:layout_width="200dip" android:layout_height="200dip" android:background="#0000FF" android:layout_gravity="center" /> <TextView android:layout_width="150dip" android:layout_height="150dip" android:background="#00BFFF" android:layout_gravity="center" /> <TextView android:layout_width="100dip" android:layout_height="100dip" android:background="#00CED1" android:layout_gravity="center" /> </merge>
改寫後再看它的樹狀圖,層次明顯更為單純。之所以如此,是因為 Activity 的根節點本身就是 FrameLayout,因此用 merge 標籤可直接附加到這個 FrameLayout,而無須再多包一層 FrameLayout。但要注意:若你的配置以 LinearLayout 等作為根節點,就不能這樣做了。
<merge/> 還有許多妙用,它和 <include/> 標籤能完美結合。<include/> 標籤用來實現程式碼重用與配置的模組化,若 UI 中需多次使用同一份配置,它便能大幅提升開發效率。以下建立一個共享配置 share.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這是一個共享配置" /> </LinearLayout>
接著在需要它的配置中使用 <include/> 標籤,並可覆寫其部分屬性(下例覆寫了 id):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <include android:id="@+id/new" layout="@layout/share"></include> </LinearLayout>
若只需要共享配置裡的內容,此時就能搭配 merge 標籤,讓配置更為高效靈活:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android"> <include android:id="@+id/newone" layout="@layout/share"></include> <include android:id="@+id/newtwo" layout="@layout/share"></include> </merge>
有了 <include/> 標籤,共享與重用配置變得輕而易舉。但實務上,一份配置中往往有許多 View 並不常用,這就造成了資源浪費。為此,Android 提供了 ViewStub 標籤來解決這個問題。
在預設情況下,ViewStub 下的標籤都帶有 visibility=GONE 屬性(不可見),更重要的是,其內容完全不佔用任何空間。ViewStub 與 include 類似,差別在於它只會在你需要時才進入畫面,並透過 inflate() 方法通知系統載入其內部的 View。如此一來,我們既能享受 <include/> 的便利,又不會產生過多用不到的 View。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <Button 8 android:id="@+id/show" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="點擊導入" 12 /> 13 <ViewStub 14 android:id="@+id/viewStub" 15 android:layout="@layout/share" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 /> 19 </LinearLayout>
對應的 Java 程式碼如下:
public class MyViewStub extends Activity {
private ViewStub mViewStub;
private Button showButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewStub = (ViewStub)findViewById(R.id.viewStub);//實體化 ViewStub,必須設定 id
showButton = (Button)findViewById(R.id.show);
showButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (mViewStub != null) {
mViewStub.inflate(); //點擊即導入 ViewStub 標籤的內容
}
}
});
}
}
當點擊按鈕後,導入的配置便會顯示出來。這裡要注意:其實很多時候我們不需要保留 ViewStub 的參照,因為一旦 ViewStub 執行了 inflate,它就會從 View 層次中被移除。深入學習後你會發現,用 ViewStub 往往比 inflate() 手動載入更為理想。但切記,ViewStub 目前尚不支援 <merge/> 標籤。
以上就是本節的分享,希望對大家有所幫助,若有問題也歡迎留言交流。
Android 應用本地化:讓 App 跟著系統語言自動切換
Android 應用程式可能在各種地區、各種語言環境的裝置上執行。想讓 App 更有親和力,就必須處理好文字、數字、檔案等內容在該語言環境下的呈現方式。 本章要解釋如何依
在Android中用ZXing打造條碼與二維碼掃描功能
智慧型手機的攝像鏡頭如今幾乎都支援自動對焦,這也為手機開啟了「掃碼」這扇大門。具備條碼掃描能力的手機,不僅能讓購物結帳更順暢,也能快速讀取電子名片等二維碼資訊。 本文示範
Java 開發環境建置(一):JDK 安裝與環境變數設定
要寫 Android,第一步是把 Java 的開發環境準備好。這一篇記錄 JDK 的安裝與 Windows 上兩個關鍵環境變數的設定流程,並附上驗證方式。 步驟一:下載
Android 控件系列:TextView 基本用法與常用屬性說明
學習目標 1. 了解 Android 中如何使用 TextView2. 掌握 TextView 的關鍵屬性 它扮演的角色 TextView 相當於其他 UI 框架裡的