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,否则不起作用。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值