Android的布局优化

Android的布局优化思想很简单,就是尽可能减少布局文件的层级,布局层级少了意味着 Android 绘制时工作量少了,性能自然就提高了.因此,我们需要删除布局中无用的层级和控件,要合理使用 LinearLayout 和 RelativeLayout ; LinearLayout 简单高效, RelativeLayout 功能强大但耗费内存,我们应该如何选择呢?

如果是一个 LinearLayout 和 RelativeLayout 都能完成的简单布局那果断是 LinearLayout .但现实往往不会这样子,单纯的 LinearLayout 是无法满足产品的需求的,往往要多个 LinearLayout 进行嵌套才能完成,这情况就使用 RelativeLayout ;布局层级的增加对产品性能消耗远比使用 RelativeLayout 要严重的多.

Android的布局优化另一个重要手段就是使用 include , merge , ViewStub 这3个标签来进行优化.下面来介绍它们的使用方法:

1.重用布局include:

include标签常用于将布局中的公共部分提取出来供其他layout共用,常用与toolbar或actionbar;
在布局文件中使用include标签:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/toolbar"/>
...
</LinearLayout>

注:可以在include标签中重写include布局android:layout的相关属性,但前提是将
android:layout_height和android:layout_width都要写上,如:

<include layout="@layout/toolbar"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      android:id="@+id/new_id"/>

2.减少布局层次merge:

merge标签一般都是和include标签配合使用,不过具体要怎么做呢?还是看代码来了解吧.
这是app主体布局:

<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=".MainActivity">

   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       />
   <include layout="@layout/include_item" />
</LinearLayout>

include_item的布局:

   <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">
     <ImageView 
      android:id="@+id/iv_pic" 
      android:src="@mipmap/ic_launcher" 
      android:layout_width="300dp" 
      android:layout_height="300dp" />
      </RelativeLayout>

此时可以使用hierarchy viewer看下app的层级视图:


older.png

可以看到有一个RelativeLayout的层级
现在将include_item改成merge:

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

     <ImageView
        android:id="@+id/iv_pic"
        android:src="@mipmap/ic_launcher"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</merge>

在看绘图布局:

new.png

可以看到,成功减少了一个布局层级;

3.高效占位符:ViewStub:

我们经常会遇到这么个情况,运行时根据情况来决定显示哪一个View或者布局;之前人们的做法往往时使用
View.Gone和View.Visitiable让View或布局叠加在一起,然后在代码中动态修改它们的可见性;

这样做的优点是逻辑简单,控制灵活;缺点是耗费资源,xml代码复杂,不利于阅读

针对这个情况,ViewStub就出现了,这是一个轻量级的View,它是一个看不见,不占布局,耗费资源极小的一个控件;
那它应该如何使用呢?看代码吧:

<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">
   <ViewStub
       android:id="@+id/subu_view"   
       android:layout="@layout/real_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

可以看到我们通过ViewStub的layout属性来让ViewStub指向了另一个布局real_view:

<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">
     <ImageView
         android:id="@+id/iv_pic"
         android:src="@mipmap/ic_launcher"
         android:layout_width="300dp"
         android:layout_height="300dp" />
</LinearLayout>

此时我们运行在手机上就只能看到空白一片,但只要我们设置下面的代码:

findViewById(R.id.subu_view).setVisibility(View.VISIBLE);

或者是:

View pic = ((ViewStub)findViewById(R.id.subu_view)).inflate();

上面两个方法都能展示ViewStub的内部布局,让内部布局替换ViewStub,一旦执行ViewStub就不再是当前布局的一部分了.如果我们还想找到ViewStub内部布局的控件,在使用第二个方法的前提下,可以这么做:

ImageView iv_pic = (ImageView) pic.findViewById(R.id.iv_pic);

搞明白用法后,那以后遇到要将两个View或布局重叠的情况就可以使用两个ViewStub了来叠加了
但是目前ViewStub有个缺陷,就是不支持merge布局;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,458评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,030评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,879评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,278评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,296评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,019评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,633评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,541评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,068评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,181评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,318评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,991评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,670评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,183评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,302评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,655评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,327评论 2 358

推荐阅读更多精彩内容