zoukankan      html  css  js  c++  java
  • Android 性能优化 四 布局优化merge标签的使用



    小白:之前分享了ViewStub标签的使用。Android还有其它优化布局的方式吗?
    小黑:<merge />标签用于降低View树的层次来优化Android的布局。先来用个样例演示一下:


    首先主须要一个配置文件activity_main.xml
    <RelativeLayout 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" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="merge标签使用" />
    
    </RelativeLayout>


    再来一个最简单的Activity,文件名称MainActivity.java
    package com.example.merge;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
         @Override
         protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
         }
    }



    小白:按着上面的代码创建project,执行后使用“DDMS -> Dump View Hierarchy for UI Automator”工具,截图例如以下
    merge 使用前



    小黑:最以下两层RelativeLayout与TextView就是activity_main.xml布局中的内容,上面的FrameLayout是Activity setContentView加入的顶层视图。以下使用merge标签能够查看下差别

    布局文件activity_main.xml改动内容例如以下:
    <merge 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" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="merge标签使用" />
    
    </merge>



    小白:使用“DDMS -> Dump View Hierarchy for UI Automator”工具。截图例如以下
    merge使用后




    小黑:FrameLayout以下直接就是TextView,与之前的相比少了一层RelativeLayout而实现的效果同样。






    某些时候,自己定义可重用的布局包括了过多的层级标签,比方我们
    比如:这种话。使用<include>包括上面的布局的时候。系统会自己主动忽略merge层级。而把两个button直接放置与include平级



    小白:什么情况考虑使用Merge标签?
    小黑:一种是向上面的样例一样。子视图不须要指定不论什么针对父视图的布局属性,样例中TextView只须要直接加入到父视图上用于显示即可。


    第二种是假如须要在LinearLayout里面嵌入一个布局(或者视图),而恰恰这个布局(或者视图)的根节点也是LinearLayout,这样就多了一层没实用的嵌套,无疑这样仅仅会拖慢程序速度。而这个时候假设我们使用merge根标签就能够避免那样的问题,官方文档Android Layout Tricks #3: Optimize by merging 中的样例演示的就是这样的情况。


    小白: <merge />标签有什么限制没?
    小黑: <merge />仅仅能作为XML布局的根标签使用。

    当Inflate以<merge />开头的布局文件时。必须指定一个父ViewGroup,而且必须设定attachToRoot为true。


    小黑:merge标签另一些属性可供使用。详细能够查看API文档,比如android:layout_width、android:layout_height等


    參考资料:
    Android Layout Tricks #2: Reusing layouts include与merge标签结合使用样例
    《Android应用性能优化》 第8章 图形



    很多其它优化相关的文章详见:《Android 基础学习文章汇总》 第三部分 性能优化



  • 相关阅读:
    Composite in Javascript
    Model Validation in Asp.net MVC
    HttpRuntime.Cache vs. HttpContext.Current.Cache
    Controller Extensibility in ASP.NET MVC
    The Decorator Pattern in Javascript
    The Flyweight Pattern in Javascript
    Model Binding in ASP.NET MVC
    Asp.net MVC
    jQuery Ajax 实例 全解析
    ASP.NET AJAX入门系列
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6958645.html
Copyright © 2011-2022 走看看