Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android UI 進階之版面配置優化(二):merge、include 與 ViewStub

編輯: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 的搭配

<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>

ViewStub:按需載入的智慧之舉

有了 <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/> 標籤。

以上就是本節的分享,希望對大家有所幫助,若有問題也歡迎留言交流。

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