使用ViewStub可以延迟加载一个布局文件,提高显示速率。刚开始接触到,记录下来。

关于viewstub的使用,我们可以在不同的布局中使用,比如可以根据设备的大小动态决定显示哪个界面。

viewstub和include比较像,都是在一个布局文件中嵌入另外一个布局文件,然而viewstub是可以说是延迟加载,它只会在你手动指定加载的时候才会加载这个布局文件,而include则会立即加载。

在布局中使用ViewStub标签来引入文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.viewstub.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="显示/隐藏" /> <ViewStub
android:id="@+id/vs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/inflated_id"
android:layout="@layout/view_stub_layout" /> </LinearLayout>

view_stub_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="vs中的tv"
/> </LinearLayout>

这是布局文件,那么怎么在程序运行时加载这个布局呢?

public class MainActivity extends ActionBarActivity {

    private ViewStub stub;
private boolean isShow = true;
private TextView tv; /* (non-Javadoc)
* @see android.support.v7.app.ActionBarActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //布局的加载有两种方式,一种是stub.inflate();
//另一种是stub.setVisibility(View.VISIBLE);
stub = (ViewStub) this.findViewById(R.id.vs);
// stub.inflate();
stub.setVisibility(View.VISIBLE);
//实例化之后就可以拿到stub布局的根节点,然后可以对之进行操作
View root = this.findViewById(R.id.inflated_id);
//注意要先实例化stub,然后才可以拿到tv
tv = (TextView) root.findViewById(R.id.tv);
root.setBackgroundColor(Color.BLUE);
} public void onClick(View v){
switch (v.getId()) {
case R.id.toggle:
if (isShow) {
stub.setVisibility(View.GONE);
}else{
stub.setVisibility(View.VISIBLE);
tv.setText("---");
}
isShow = !isShow;
break; default:
break;
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

android开发布局优化之ViewStub的更多相关文章

  1. Android开发性能优化总结(一)

    安卓开发应用首先要讲究良好的用户体验,如果一款软件卡顿现象严重,不流畅,经常崩溃,那么将给用户带来极不良好的体验,从而损失用户. 在实际开发和学习中,我总结了一下关于安卓性能的优化,供大家参考交流. ...

  2. 【转】Android布局优化之ViewStub

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  3. Android UI学习 - FrameLayou和布局优化(viewstub)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/308090 Fram ...

  4. Android开发——布局性能优化的一些技巧(一)

    0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...

  5. [Android]Android布局优化之 ViewStub

    转载请标注:转载于http://www.cnblogs.com/Liuyt-61/p/6602926.html -------------------------------------------- ...

  6. Android开发——布局性能优化的一些技巧(二)

    , 0, drawable.getMinimumWidth(),dra.getMinimumHeight()); tv.setCompoundDrawables(null, null, drawabl ...

  7. (转) Android开发性能优化简介

    作者:贺小令 随着技术的发展,智能手机硬件配置越来越高,可是它和现在的PC相比,其运算能力,续航能力,存储空间等都还是受到很大的限制,同时用户对手机的体验要求远远高于PC的桌面应用程序.以上理由,足以 ...

  8. [素材资源] Android开发性能优化简介(非常不错的)

    转自(http://www.starming.com/index.php?action=plugin&v=wave&tpl=union&ac=viewgrouppost& ...

  9. 姿势摆好,一招学会android的布局优化!

    作为android应用来讲,无论应用本身多么美观,功能多么强大,内容多么丰富.但如果App本身打开界面缓慢超过手机16ms刷新一次页面的时间,就会产生卡顿.用户体验都会变得极差,导致用户量减少.所以我 ...

随机推荐

  1. Penetration test

    Contents 1 History 2 Standards and certification 3 Tools 3.1 Specialized OS distributions 3.2 Softwa ...

  2. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  3. 【Spring】Spring IOC原理及源码解析之scope=request、session

    一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...

  4. Android Animation初识

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  5. 利用if else 求已发奖金总数

    class Program    {        static void Main(string[] args)        {            while (true)           ...

  6. MVC3 IIS7部署记录

    环境 系统:Win7 IIS:IIS7 开发工具:VS2010 MVC版本:ASP.NET MVC 3.0 注意 1.在部署MVC应用之前,一定要确保你的程序BIN文件夹下面是否包含 System.W ...

  7. 非sqlite和nigix的开源c项目

    1.http://code.google.com/p/friso/ 一.friso中文分词器 Friso是使用c语言开发的一款高性能中文分词器,使用流行的mmseg算法实现.完全基于模块化设计和实现, ...

  8. An exception occurred during configuration of persistence layer.

    配置文件放在bin文件夹下(注意:hibernate.cfg.xml文件名不要随便改动)

  9. .NET混淆工具 (Dotfuscator Professional Edition)

      如果不想自己辛辛苦苦码出来的作品被人轻易的破解cpoy就使用这款工具试试吧.^_^   使用版本:4.9.7500.9484 ,破解版:http://pan.baidu.com/s/1dDH7lr ...

  10. MVC 依赖注入/控制反转

    http://www.cnblogs.com/cnmaxu/archive/2010/10/12/1848735.html http://www.cnblogs.com/artech/archive/ ...