使用延迟加载以及避免代码重复
​一.概要:
    <include />标签是整理布局的有效工具,提供了合理组织XML布局文件的有效方法。
    ViewStub是实现延迟加载视图的优秀类。无论在什么情况下,只要开发者需要根据上下文选择隐藏或则显示一个视图,都可以使用ViewSub实现。
    或许并不会因为一个视图的延迟加载而感觉到性能的明显提升,但是如果视图树的层次很深,便会感觉到性能上的 差距了。
二.代码:
  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" /> <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

 <?xml version="1.0" encoding="utf-8"?>
<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" />

  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);
} @Override
protected boolean isRouteDisplayed() {
return false;
}
}

  Ps:对于<include />中用到的android:layout_width和android:layout_height的属性在被引用的布局文件中要申明为0;

   

HackTwo的更多相关文章

随机推荐

  1. innodb事务日志详解

    首先看InnoDB的缓存和文件的关系图如下: InnoDB事务日志功能介绍 InnoDB使用日志来减少提交事务时的开销.因为日志中已经记录了事务,就无须在每个事务提交时把缓冲池的脏块刷新(flush) ...

  2. An internal error occurred during: "Map/Reducelocation status updater".java.lang.NullPointerException

    当我们运行wordcount代码时,出现报错,如下所示: An internal error occurred during: "Map/Reducelocation status upda ...

  3. 获取access_token错误 40164

    没有添加IP白名单

  4. flask系列三之Jinja2模板

    1.如何渲染模板 模板在‘templates’文件夹下(htnl页面) 从flask中导入render_template函数---渲染html模板 在视图函数中,使用render_template 函 ...

  5. stm32中断 抢占优先级 和 响应优先级 有什么区别

    与51不同,stm32的中断分类更灵活.51只是按先后顺序大小排列互相打断. stm32中多了响应优先级这一概念. stm32的中断分为 1.抢占(占先)优先级. 2.响应优先级. 1.抢占优先级.抢 ...

  6. 监控和安全运维 1.7 nagios配置邮件告警

    8. 配置邮件告警 服务端 vim /etc/nagios/objects/contacts.cfg 增加: define contact{ contact_name use generic-cont ...

  7. Class.forName和ClassLoader.loadClass区别(转)

    Java中class是如何加载到JVM中的:1.class加载到JVM中有三个步骤    装载:(loading)找到class对应的字节码文件.    连接:(linking)将对应的字节码文件读入 ...

  8. HTML_基础篇v2

    网站图片页面显示案例 1.需求分析 需要在浏览器中显示2张图片信息,效果如下: 2.技术分析 [图片标签]<img /> 属性: src:指图片的位置(路径) 路径的写法:绝对路径和相对路 ...

  9. ZigBee协议栈中AES加密算法

    原文地址:ZigBee协议栈中AES加密算法作者:大浪淘沙 Z-stack对Zigbee2006提供了全面的支持,功能之强大,性能稳定.安全性高,说到安全性是我们今天的主题.CC2430硬件支持128 ...

  10. tomcat配置多个host

    当一个tomcat需要配多个应用时,并且内网和外网的访问IP还不一样,就需要使用到tomcat配置多个虚拟主机. <Host name="localhost"  appBas ...