Android 佈局優化--include標籤

性能優化之一就是layout的優化,

as 常識:

佈局是否合理主要影響的是頁面測量時間的多少,我們知道一個頁面的顯示測量和繪製過程都是通過遞歸來完成的,多叉樹遍歷的時間與樹的高度h有關,其時間複雜度 O(h),如果層級太深,每增加一層則會增加更多的頁面顯示時間,所以佈局的合理性就顯得很重要。

那佈局優化有哪些方法呢,主要通過減少層級、減少測量和繪製時間、提高複用性三個方面入手。總結如下:

  • 減少層級。合理使用 RelativeLayout 和 LinerLayout,合理使用Merge。
  • 提高顯示速度。使用 ViewStub,它是一個看不見的、不佔佈局位置、佔用資源非常小的視圖對象。
  • 佈局複用。可以通過includ標籤來提高複用。
  • 儘可能少用wrap_content。wrap_content 會增加布局 measure 時計算成本,在已知寬高爲固定值時,不用wrap_content 。
  • 刪除控件中無用的屬性

第一點關於merge已經在上一篇做了介紹,這裏先看一下第三點:佈局複用。可以通過includ標籤來提高複用。

就是寫了一個佈局,使用的地方比較多,在每個使用它的佈局的地方,使用include就可以了,不用重複寫代碼了。

這個比較簡單,看源碼的例子:

 xref: /sprdroid8.1_trunk/development/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/immersive_sticky_activity.xml

    HomeHistoryAnnotateLine#Navigate Raw Download 

    only in /sprdroid8.1_trunk/development/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/ 

1<!--
2  Copyright 2013 The Android Open Source Project
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  -->
16
17<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
18    android:layout_width="match_parent"
19    android:layout_height="match_parent">
20
21    <include layout="@layout/include_content" />
22
23</FrameLayout>
24

 xref: /sprdroid8.1_trunk/development/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/include_content.xml

    HomeHistoryAnnotateLine#Navigate Raw Download 

    only in /sprdroid8.1_trunk/development/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/ 

1<!--
2  Copyright 2013 The Android Open Source Project
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  -->
16
17<!-- The primary full-screen view. This can be replaced with whatever view
18     is needed to present your content, e.g. VideoView, SurfaceView,
19     TextureView, etc. -->
20<TextView xmlns:android="http://schemas.android.com/apk/res/android"
21    android:id="@+id/fullscreen_content"
22    android:layout_width="match_parent"
23    android:layout_height="match_parent"
24    android:keepScreenOn="true"
25    android:textColor="#fb3"
26    android:fontFamily="sans-serif-condensed"
27    android:textStyle="bold"
28    android:lineSpacingMultiplier="0.8"
29    android:textSize="50sp"
30    android:gravity="center"
31    android:text="@string/placeholder_content" />

使用注意:

include標籤若指定了ID屬性,而你的layout也定義了ID,則你的layout的ID會被覆蓋。如果findViewById()查找layout的Id來查找子控件,會出現這種情況。

如果對inlude增加其他layout屬性等,需要先寫layout_height和layout_width,否則不起作用。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章