使用延迟加载以及避免代码重复
​一.概要:
    <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. qt的下载地址

    上Qt官网http://www.qt.io/download/想下载Qt,速度很慢,在这里记录下在Qt官网看到的镜像下载地址: 1. 所有Qt版本下载地址: http://download.qt.io ...

  2. 1078 Hashing

    题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Sea ...

  3. python开发socket套接字:套接字&通信循环&链接循环&模拟远程命令

    一,套接字 先从服务器端说起.服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接.在这时如果有个客户端初始化一个Socket ...

  4. python学习(十一) 文件和流

    11.1 打开文件 >>> f = open(r'c:\text\somefile.txt'),  第一个参数是文件名,必须有:第二个是模式:第三个参数是缓冲. 11.1.1 文件模 ...

  5. 【linux】查看进程使用的端口和端口使用情况

    netstat -a 查看所有服务端口 netstat -tln 查看当前使用的端口   ps命令查看进程的id: ps aux | grep ftp 或者 pidof Name   netstat命 ...

  6. python签名设计

    将一个签名网站http://www.uustv.com/的内容爬下来显示出来 代码:sign.py from tkinter import * from tkinter import messageb ...

  7. Visual Studio 2005 C# 读写Excel文件

    做作业的时候查了一点儿资料, 用的vs2k5 读 excel 发现用起来非常简单...现在编程语言没话说! 项目-添加引用-COM-Microsoft Excel 12.0 Object Librar ...

  8. leetcode554

    public class Solution { public int LeastBricks(IList<IList<int>> wall) { ) { ; } ; Dicti ...

  9. MFC 打开外部EXE文件的三种方法

    目前知道三种方式:WinExec,ShellExecute ,CreateProcess,别人已经总结的很好了<vc中调用其他应用程序的方法(函数) winexec,shellexecute , ...

  10. 刷题向》DP》放苹果 (normal)

    这篇博客可能字数比较多,而且很难讲清楚,我会努力给你们讲清楚: 首先,放苹果是一道DP,之所以难,是因为很难想到,我的确有同学用三维数组做出来,然而三维的的确比二维好理解,但三维复杂度太高,虽然DP一 ...