Android使用include/merge/ViewStub优化布局

一、使用include标签将可复用的组件抽取出来(引用布局)

二、使用merge标签减少布局的嵌套层次(merge相当于framelayout)

场景1:布局根结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替。
场景2:某布局作为子布局被其他布局include时,使用merge当作该布局的顶节点,这样在被引入时,顶结点会自动被忽略。

三、使用ViewStub标签来加载一些不常用的布局

作用:ViewStub标签同include标签一样可以用来引入一个外部布局,不同的是,ViewStub引入的布局默认不会扩张,既不会占用显示也不会占用位置,从而在解析Layout时节省cpu和内存

示例一:include


下面是res/layout/title.xml 布局文件:

<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="wrap_content"  
    android:background="@drawable/title_bg" >  
 
   <Button  
        android:id="@+id/title_back"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:layout_margin="5dp"  
        android:background="@drawable/back_bg"  
        android:text="Back"  
        android:textColor="#fff" />  
 
   <TextView  
        android:id="@+id/title_text"  
        android:layout_width="0dp"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:layout_weight="1"  
        android:gravity="center"  
        android:text="This is Title"  
        android:textColor="#fff"  
        android:textSize="25sp" />  
 
   <Button  
        android:id="@+id/title_edit"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:layout_margin="5dp"  
        android:background="@drawable/edit_bg"  
        android:text="Edit"  
        android:textColor="#fff" />  
 
 

下面是res/layout/activity_main.xml 布局文件:

<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="wrap_content" >  
 
    
 

示例二:merge


下面是res/layout/progress.xml 布局文件:

 
<merge xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
 
   <ProgressBar  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center" />  
 
 

下面是res/layout/activity_main.xml 布局文件:

<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="wrap_content"  
    android:orientation="vertical" >  
 
   <FrameLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
 
       <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="正文内容"  
            android:textSize="25sp" />  
 
        
    
 
 

示例三:ViewStub

下面是res/layout/activity_main.xml 布局文件:

<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="wrap_content"  
    android:orientation="vertical" >  
 
   <Button   
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="显示隐藏内容"/>  
      
   <ViewStub   
        android:id="@+id/id_viewStub"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout="@layout/title"/>  

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {  
    private Button b;  
    private ViewStub stub;  
 
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        b=(Button) findViewById(R.id.button);  
        stub=(ViewStub) findViewById(R.id.id_viewStub);  
        b.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                stub.inflate();  
            }  
        });  
    }  

时间: 2024-08-01 04:20:09

Android使用include/merge/ViewStub优化布局的相关文章

Android自定义实现BaseAdapter的优化布局_Android

上一篇中我们介绍了自定义实现BaseAdapter的普通实现布局,然而上一章也说了普通实现的方式效率会很低,而且对系统开销也很大,所以,那样的实现是为了让初学者能知道可以这样使用,在实际项目中不可能使用那种方式的,要是你在做项目的时候使用普通布局方式,我敢保证,不过试用期你的老板就给你飞机票走人了,好了,闲话少说,本次讲解一下优化布局的实现,看完代码后,你会觉得,其实很简单. MainActivity.java public class MainActivity extends AppCompa

Android自定义实现BaseAdapter的优化布局

上一篇中我们介绍了自定义实现BaseAdapter的普通实现布局,然而上一章也说了普通实现的方式效率会很低,而且对系统开销也很大,所以,那样的实现是为了让初学者能知道可以这样使用,在实际项目中不可能使用那种方式的,要是你在做项目的时候使用普通布局方式,我敢保证,不过试用期你的老板就给你飞机票走人了,好了,闲话少说,本次讲解一下优化布局的实现,看完代码后,你会觉得,其实很简单. MainActivity.java public class MainActivity extends AppCompa

Android开发之merge结合include优化布局_Android

merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来. 布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们可以使用merge作为根节点,这样可以减少布局嵌套,提高显示速率. <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://sch

Android开发之merge结合include优化布局

merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来. 布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们可以使用merge作为根节点,这样可以减少布局嵌套,提高显示速率. <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://sch

Android中使用ViewStub实现布局优化_Android

在Android开发中,View是我们必须要接触的用来展示的技术.通常情况下随着View视图的越来越复杂,整体布局的性能也会随之下降.这里介绍一个在某些场景下提升布局性能的View,它就是ViewStub. ViewStub是什么 ViewStub是View的子类 它不可见,大小为0 用来延迟加载布局资源 注,关于Stub的解释 A stub is a small program routine that substitutes for a longer program, possibly to

Android使用ViewStub提高布局性能

在Android开发中,View是我们必须要接触的用来展示的技术.通常情况下随着View视图的越来越复杂,整体布局的性能也会随之下降.这里介绍一个在某些场景下提升布局性能的View,它就是ViewStub. ViewStub是什么 ViewStub是View的子类 它不可见,大小为0 用来延迟加载布局资源 注,关于Stub的解释 A stub is a small program routine that substitutes for a longer program, possibly to

Android的include中,怎么获取不同include中的相同布局中的控件id

问题描述 Android的include中,怎么获取不同include中的相同布局中的控件id 比如,我的layout.xml中只有一个ImageButton 在另一个tablelayout.xml中通过include调用layout.xml 但是我想在activity中获取不同include中的imageButton的ID 解决方案 系统不会管你在layout中声明了多少个相同ID的View的,通过findViewByID查找控件时,它只关注子View内是否有符合ID的View,其他同ID的V

Android高级开发之性能优化典范_Android

本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介     Overdraw就是过度绘制,是指在一帧的时间内(16.67ms)像素被绘制了多次,理论上一个像素每次只绘制一次是最优的,但是由于重叠的布 局导致一些像素会被多次绘制,而每次绘制都会对应到CPU的一组绘图命令和GPU的一些操作,当这个操作耗时超过16.67ms时,就会出现掉帧现象,表现为应用卡顿,所以对重叠不可见元素的重复绘制会产生额外的开销,需

Android开发技巧之ViewStub控件惰性装载_Android

在4.5.6节介绍过一个<include>标签,该标签可以在布局文件中引用另外一个布局文件,并可以覆盖被引用布局文件根节点所有与布局相关的属性,也就是以android:layout开头的属性.通过<include>标签可以将一个非常庞大的布局文件分解成若干个较小的布局文件,而且这些小的布局文件也可以被多次引用,从而达到一个重用的目的. <include>标签固然很好用,但有一个问题,就是布局文件中的控件并不一定在程序启动时全都用到,有一些控件只在特定的情况下才会被使用到