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

淺談 Android 布局優化的三大標簽:include、merge、ViewStub

編輯:關於 Android 程式設計

1. 布局重用:<include />

include 標簽能夠重用布局文件,簡單用法如下:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/app_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <include layout="@layout/titlebar" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/hello" />
    ...
</LinearLayout>

要點:
1)layout 屬性是必須的。
2)若 include 指定了 id,而被引入的 layout 也定義了 id,則原 layout 的 id 會被覆蓋,需注意。
3)include 中所有 android:layout_* 屬性都有效,前提是必須同時寫 layout_width 和 layout_height。
4)佈局中可包含兩個相同的 include,引用時可用 findViewById(R.id.bookmarks_favourite).findViewById(R.id.bookmarks_list) 的方式區分。

2. 減少視圖層級:<merge />

顧名思義,merge 就是「合並、融合」的意思,能有效把某些多餘的層級優化掉。主要用於兩種場合:
(1)自定義 View 中,父元素盡量是 FrameLayout(若非 FrameLayout 但不太複雜也可用);
(2)Activity 整體佈局的根元素需要是 FrameLayout。

下面這個例子融合了兩種情況,展示如何縮減層次。整體界面如下:

其中粉紅色圈住的部分為自定義 View。界面佈局 layout_mergedemo.xml:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="我的家園"
        android:textColor="#FF0000FF"
        android:textSize="25sp" />

    <com.example.myandroiddemo.MyItemView
        android:id="@+id/myitem_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

自定義佈局 view_item.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/view_item_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/view_item_txt"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:textColor="#FFFF0000"
        android:textSize="20sp" />
</LinearLayout>

自定義 View 中解析佈局:

public class MyItemView extends LinearLayout {
    ...
    private void initView(Context context) {
        mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_item, this, true);
        mMyItemImg = (ImageView) view.findViewById(R.id.view_item_img);
        mMyItemText = (TextView) view.findViewById(R.id.view_item_txt);
    }
    ...
}

開發完成後用 HierarchyViewer 查看層次:

可以發現,簡單的功能竟用了六層佈局(含每個 Window 自動添加的 PhoneWindow$DecorView 和 FrameLayout(id/content))。其中第二、三層的 FrameLayout,第四層的 MyItemView(LinearLayout 子類) 與第五層的 LinearLayout 都存在冗餘,可以縮減。

此時該 merge 大顯身手了。簡化後:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="我的家園"
        android:textColor="#FF0000FF"
        android:textSize="25sp" />

    <com.example.myandroiddemo.MyItemView
        android:id="@+id/myitem_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</merge>

由於此處 merge 替代的是 LinearLayout 而非 FrameLayout,所以需在自定義代碼中補上 LinearLayout 的屬性(如方向、背景等):

private void initView(Context context) {
    setOrientation(LinearLayout.HORIZONTAL);
    ...
}

再來看層次:

只剩四層了,加載速度也明顯加快。佈局越複雜、子 View 越多,合理使用 merge 越能提升流暢性。但 merge 也有使用限制:
(1)只能作為佈局 XML 文件的根元素;
(2)使用 merge inflate 時,必須指定一個 ViewGroup 作為父元素,且 attachToRoot 參數為 true;
(3)不能在 ViewStub 中使用 merge 標簽。

另外要提醒的是,Window(如 Activity)加載時會自動添加兩層佈局,若想在根元素使用 merge 的同時設置整體背景,可取出 (id/content) 的 FrameLayout 再設屬性:

FrameLayout frameLayout = (FrameLayout) this.getWindow().getDecorView()
        .findViewById(android.R.id.content);
frameLayout.setBackgroundResource(R.drawable.bg_repeated_main);
LayoutInflater.from(this).inflate(R.layout.layout_showset, frameLayout, true);

3. 按需加載:<ViewStub />

ViewStub 是一個輕量級的頁面,通常用於預加載,以改善加載速度和流暢性。它本身不佔用層級,最終會被它指定的佈局取代。

在實際項目中,由於動畫較多、佈局嵌套複雜,低端機切換頁面時常感卡頓。為此,可在切換動畫期間只放一個 loading 圖標和一個 ViewStub:

layout_loading.xml:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <NetErrAndLoadView
        android:id="@+id/start_loading_lay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</merge>

這個頁面非常輕量,加載動畫因此又快又流暢。等動畫結束後,再指定 ViewStub 的資源加載實際頁面:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_loading);
    mLoadHandler = new Handler();
    mLoadingView = (NetErrAndLoadView) findViewById(R.id.start_loading_lay);
    mLoadingView.startLoading();
    mViewStub = (ViewStub) findViewById(R.id.viewstub);
    mViewStub.setLayoutResource(R.layout.layout_main);
    mLoadHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mViewStub.inflate();
            mLoadingView.hide();
        }
    }, 500);
}

其中 500 為延遲加載的毫秒數。也可以在 ViewStub 佈局中直接設置 android:layout 屬性來指定資源。

ViewStub 也有幾點小缺點:
1. 只能 inflate 一次,之後 ViewStub 對象會被置空,不適用於需要反覆按需顯示隱藏的場景;
2. 只能 inflate 一個佈局文件,若想操作具體 View,還是直接用 visibility 屬性更方便;
3. ViewStub 中不能嵌套 merge 標簽。

儘管如此,這些都不無傷大雅,ViewStub 依然能幫我們做很多事。不得不佩服 Google Android 團隊的架構前瞻性,連這樣細微的設計都考慮到了。

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