当你在Application中创建复杂的布局时。页面的渲染过程也变得更加缓慢。

此时,我们须要利用 <include />标签(避免反复渲染)和 ViewStub类(延迟载入)来优化我们的页面。
一、利用<include />标签来避免反复渲染

当我们须要为App中的每一个View都加入一个header或者footer时,你会怎么做?
反复地复制粘贴能够解决问题,但未免太繁杂。能够试着使用<include />标签:

第一种方式。在<include />标签内指定width及height:
main.xml

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" > <Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" /> <include
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_alignParentBottom ="true"
android:layout_marginBottom ="30dp"
layout ="@layout/footer" /> </RelativeLayout>
footer.xml
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "0dp"
android:layout_height= "0dp"
android:gravity= "center"
android:text= "@string/footer_text" />
有个小细节须要注意。在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义

另外一种方式。直接引用:
main.xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" > <Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" /> <include layout ="@layout/footer" /> </RelativeLayout>
footer.xml
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_alignParentBottom= "true"
android:layout_marginBottom= "30dp"
android:gravity= "center"
android:text= "@string/footer_text" />
二、利用ViewStub类来延迟载入视图
在设计视图时,有时会考虑到某些视图的可见性是依赖于用户的操作或者执行设备的详细环境的。
此时你会怎样设计?不过改变View的visible属性?
我们先来看看ViewStub的介绍:
   
 ViewStub是一个不可见、不占空间(zero-sized)的控件。它能够用来在执行时延迟载入视图资源。仅仅有当我们将ViewStub的可见性设为true。或者调用inflate()方法,它的视图资源才会被载入。

如果我们设计的一个页面中包括地图View,试想一下下面这样的情况:
     有些用户不须要看到地图。

既然用户不须要看到地图,我们为何还坚持不懈地载入它?这反而影响了我们App的Performance。
此时,我们就能够利用ViewStub类了:
main.xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" > <Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" /> <ViewStub
android:id ="@+id/map_stub"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:inflatedId ="@+id/map_view"
android:layout ="@layout/map" />
</RelativeLayout>

map.xml

<com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:apiKey= "my_api_key"
android:clickable= "true" />
接下来看看MainActivity
public class MainActivity extends MapActivity {

  private View mViewStub;

  @Override
public void onCreate (Bundle savedInstanceState ) {
super. onCreate( savedInstanceState );
setContentView( R. layout. main);
mViewStub = findViewById( R. id. map_stub);
} public void onShowMap (View v) {
mViewStub. setVisibility (View .VISIBLE );
}
....
}
如你所见。在须要显示图像时我们才调用onShowMap来改变map_stub的可见性。在改变之前。map_stub都不会渲染载入视图资源。

小结:
     1.当我们的页面变得复杂,XML文件内容过多时。<include />标签能够有效地帮助我们整理文件内容,同一时候提高了XML文件的可读性。同一时候,它的使用方法也与Fragment类似。
     2.ViewStub是一个极佳的延迟载入视图资源的方式。仅仅要你设计的视图是依赖于上下文来改变其可见性的,就利用ViewStub类吧。

或许当你仅仅将其应用在一个简单的页面其中时,并不会感觉到在性能上有不论什么提升,可是在复杂页面中,它的效果是极佳的。

50个Android开发技巧(02 延迟载入和避免反复渲染视图)的更多相关文章

  1. 50个android开发技巧

    50个android开发技巧 http://blog.csdn.net/column/details/androidhacks.html

  2. 50个Android开发技巧(24 处理ListView数据为空的情况)

         在移动平台上为用户展示数据的一个经常用法是将数据填充进一个List内,而此时须要注意的一点就是: 原文地址:(http://blog.csdn.net/vector_yi/article/d ...

  3. 50个Android开发技巧(11 为文字加入特效)

    问题:怎样构建一个模拟LED数字时钟的页面?效果例如以下图所看到的: (原文地址:http://blog.csdn.net/vector_yi/article/details/24460227) 分析 ...

  4. 50一个Android开发技巧(01 利用好layout_weight属性)

    问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%? 分析:问题想要达到的效果应该是这样: (原文地址:http://blog.csdn.net/vector_yi/art ...

  5. 50个Android开发技巧(03 自己定义ViewGroup)

    问题:怎样创建一个例如以下图所看到的的布局?                图1 (原文地址:http://blog.csdn.net/vector_yi/article/details/244155 ...

  6. 50个Android开发技巧(12 为控件加入圆角边框)

    控件的圆角边框能够使你的App看起来更美观,事实上实现起来也非常easy. (原文地址:http://blog.csdn.net/vector_yi/article/details/24463025) ...

  7. 50个Android开发技巧(09 避免用EditText对日期进行验证)

    我们都知道,在表单中对数据进行验证不但无聊并且easy出错. (原文地址:http://blog.csdn.net/vector_yi/article/details/24424713) 想象一下,一 ...

  8. 50个Android开发技巧(10 为TextView加入样式)

    首先来看一个控件的例子: (原文地址:http://blog.csdn.net/vector_yi/article/details/24428085) 手机上类似这种场景你一定已经见过非常多次了,但有 ...

  9. 50个Android开发人员必备UI效果源码[转载]

    50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面 ...

随机推荐

  1. NOI2003 逃学的小孩

    这一题不会做啊…… 我觉得真要比赛的话我可能会随机上几万次,然后再用LCA求距离,更新最优值,等到快超时的时候输出答案…… 题解请看2007年陈瑜希论文 代码: ; type node=record ...

  2. Vs 引用第三方DLL文件 版本不一致问题 (npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案)

    最近在 做 MailChimp 与网站功能 集成时,发现 MailChimp 2API 中的 MailChimp.dll  中的依赖项 SerivceStack.Text.dll (版本为3.9.71 ...

  3. SQLite Helper (C#) zt

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp This small class (SQLiteHelper.cs) i ...

  4. as3+java+mysql(mybatis) 数据自动工具(七) - 完结

    autoscript packed 文件地址:http://pan.baidu.com/s/1dDvgcO5 如果需要项目源码的话,可以留下邮箱,先声明一下,该工具主要是为了实现自动同步输出代码类文件 ...

  5. suds 在python3.x上的安装并访问webservice

    类库安装 直接使用命令行:pip install suds 报错:Traceback (most recent call last):    File "setup.py", li ...

  6. wuzhicms私密下载链接生成

    加载函数库:load_function('content','content'); echo private_file('http://dev.wuzhicms.com/uploadfile/2014 ...

  7. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  8. Cppcheck代码分析(2)

    功能 解析函数中的可能的代码执行流 函数实际执行中只会执行代码流中的一条流 分析: 分支语句 if-else ,switch-case 循环语句 while, do-while ,for 代码流举例 ...

  9. ACM2026

    /* 首字母变大写 Problem Description 输入一个英文句子,将每个单词的第一个字母改成大写字母.   Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句 ...

  10. jqgrid在页面出来竖型滚动条自动调整列宽

    在项目中使用jqgrid的时候,需要设置在页面竖型滚动条出来的时候,列宽进行调整 1. 判断jqgrid的宽度是否和页面的宽度不一致(判断滚动条是否出来) 2. 调整jqgrid的列宽,因为jqgri ...