美文网首页我的Android之路android开发Android
android性能优化-渲染-嵌套标签

android性能优化-渲染-嵌套标签

作者: DanielHan | 来源:发表于2015-08-08 21:29 被阅读1729次

1.重用布局include

include标签常用于将布局中的公共部分提取出来供其他layout共用,以实现布局模块化,这在布局编写方便提供了大大的便利。例如,每个activity都会用到的titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
</FrameLayout>

用include引用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/titlebar"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>
注: 可以在include标签中覆盖被引用的根布局的android:layout_*属性,但是前提是android:layout_height和android:layout_width都要写,如:
<include  android:id=”@+id/news_title” 
          android:layout_width=”match_parent”
          android:layout_height=”match_parent”         
          layout=”@layout/title”/>

2.减少布局层次merge

merge标签可用于两种典型情况:

  1. 布局顶结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity内容视图的parent view就是个FrameLayout,所以可以用merge消除只剩一个。
  2. 某布局作为子布局被其他布局include时,使用merge当作该布局的顶节点,这样在被引入时顶结点会自动被忽略,而将其子节点全部合并到主布局中。
    用hierarchy viewer查看main.xml布局如下图
1.png

如图,红色部分的RelativeLayout是多余的,解决如下:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:layout_above="@+id/text"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:layout_alignParentBottom="true"
        android:text="@string/app_name" />

</merge>

结果如下图:

2.png
注:listview的item布局根目录不能用merge,否则会报错:

merge can be used only with a valid ViewGroup root and attachToRoot=true

3.需要时加载ViewStub

viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。
viewstub常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等。
定义如下:

<ViewStub
    android:id="@+id/stub_import"
    android:inflatedId="@+id/panel_import"
    android:layout="@layout/stub_import"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

加载ViewStub:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

一旦visible/inflated,ViewStub元素将被layout属性值替换,并且android:inflatedId的值将作为根布局的id

注:
1. ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
2. 要想获得stub_import中的控件,用如下代码
  //必须使用inflate方法,否则下面获取不到子控件
  View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
  importPanel.findViewById(R.id.children);

另外欢迎关注我的:
Github
微博
掘金
微信公众号

蛋妞码农

相关文章

  • android性能优化-渲染-嵌套标签

    1.重用布局include include标签常用于将布局中的公共部分提取出来供其他layout共用,以实现布局模...

  • Android优化文章精选

    Android性能优化典范 Android性能优化典范 - 第1季Android性能优化之渲染篇Android性能...

  • Android优化篇

    Android性能优化之渲染篇 - [ Android性能优化篇 [ 谷歌官方 ] ] - 看云 Android性...

  • Android中的卡顿现象

    Android性能优化三个方面 Android的渲染机制,内存与GC,电量优化 1.Android渲染机制 垂直同...

  • 性能优化文章

    Android性能优化之渲染篇:http://hukai.me/android-performance-rende...

  • 安卓性能优化

    Android的性能优化方法 1. 布局优化 使用 标签、标签、 控件 复杂布局...

  • Android群英传读书笔记(第十章)

    上一章 本章主要介绍的是android的性能优化。 1.布局优化 Android UI渲染机制在Android中,...

  • Android面试宝典 - 优化篇

    一、性能优化 1. 布局优化 尽量使用include,merge,ViewStub标签避免冗余嵌套以及过于复杂的布...

  • Android性能优化实践总结

    系列阅读Android 性能优化典范(一):主要从 Android 的渲染机制、内存与 GC、电量优化三个方面展开...

  • 前端性能优化

    优化项目加载速度,首屏渲染速度。 html优化使用 声明减少嵌套层级减少不必要的Dom元素标签...

网友评论

本文标题:android性能优化-渲染-嵌套标签

本文链接:https://www.haomeiwen.com/subject/kozbqttx.html