详解Android布局优化

详解Android布局优化,第1张

概述怎样才能写出优秀的AndroidApp,是每一个程序员追求的目标。那么怎么才能写出一个优秀的App呢?相信很多初学者也会有这种迷茫。一句话来回答这个问题:细节很重要。今天我们就从最基础的XML布局来谈谈怎么提高Androi

怎样才能写出优秀的AndroID App,是每一个程序员追求的目标。那么怎么才能写出一个优秀的App呢?相信很多初学者也会有这种迷茫。一句话来回答这个问题:细节很重要。今天我们就从最基础的XML布局来谈谈怎么提高AndroID性能问题吧!

也许你经常会遇到比较复杂的布局,这种情况下,最简单的方法就是多层嵌套实现效果,但是最简单的方法是否是最优的方法呢? 这里需要打一个大大的问号?????经验告诉我们,往往简单的方法,得到的结果不是最优解,那么我们通过一个例子来研究一下怎么去优化我们的XML布局吧,下面通过经典微信中的“发现”tab页面中的布局来看看怎么实现。

上面这张图片是微信界面截图,看到这张效果图的第一眼会让开发者想到使用线性布局实现这种左边图片,右边文字,一行白色背景效果很方便。那么我们就按照一般思路写出如下布局代码:

<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:background="@color/color_eeeeee" androID:orIEntation="vertical" tools:context=".MainActivity"> <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="0dp"  androID:layout_weight="1"  androID:orIEntation="vertical">  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_margintop="20dp"   androID:background="@drawable/item_bg_select"   androID:clickable="true"   androID:gravity="center_vertical"   androID:orIEntation="horizontal"   androID:paddingleft="20dp"   androID:paddingRight="20dp">   <ImageVIEw    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:src="@drawable/afe" />   <TextVIEw    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_marginleft="15dp"    androID:text="@string/fIEnds"    androID:textcolor="@androID:color/black"    androID:textSize="16sp" />  </linearLayout>  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_margintop="20dp"   androID:background="@androID:color/white"   androID:orIEntation="vertical">   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/afg" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/scan"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>   <VIEw    androID:layout_wIDth="match_parent"    androID:layout_height="0.5dp"    androID:layout_marginleft="10dp"    androID:layout_marginRight="10dp"    androID:background="@color/color_e0e0e0"></VIEw>   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/afh" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/shake"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>  </linearLayout>  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_margintop="20dp"   androID:background="@androID:color/white"   androID:orIEntation="vertical">   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/afd" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/nearby"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>   <VIEw    androID:layout_wIDth="match_parent"    androID:layout_height="0.5dp"    androID:layout_marginleft="10dp"    androID:layout_marginRight="10dp"    androID:background="@color/color_e0e0e0"></VIEw>   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/afb" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/float_bottle"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>  </linearLayout>  <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_margintop="20dp"   androID:background="@androID:color/white"   androID:orIEntation="vertical">   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/agg" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/shop@R_502_6817@"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>   <VIEw    androID:layout_wIDth="match_parent"    androID:layout_height="0.5dp"    androID:layout_marginleft="10dp"    androID:layout_marginRight="10dp"    androID:background="@color/color_e0e0e0"></VIEw>   <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/item_bg_select"    androID:clickable="true"    androID:gravity="center_vertical"    androID:orIEntation="horizontal"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <ImageVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:src="@drawable/ak6" />    <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="15dp"     androID:text="@string/games"     androID:textcolor="@androID:color/black"     androID:textSize="16sp" />   </linearLayout>  </linearLayout> </linearLayout> <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:background="@androID:color/white"  androID:orIEntation="horizontal">  <TextVIEw   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1"   androID:drawabletop="@drawable/ala"   androID:gravity="center"   androID:text="@string/weixin"   androID:textcolor="@color/color_9e9e9e" />  <TextVIEw   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1"   androID:drawabletop="@drawable/al9"   androID:gravity="center"   androID:text="@string/countans"   androID:textcolor="@color/color_9e9e9e" />  <TextVIEw   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1"   androID:drawabletop="@drawable/alc"   androID:gravity="center"   androID:text="@string/finds"   androID:textcolor="@color/color_9e9e9e" />  <TextVIEw   androID:layout_wIDth="0dp"   androID:layout_height="wrap_content"   androID:layout_weight="1"   androID:drawabletop="@drawable/ale"   androID:gravity="center"   androID:text="@string/me"   androID:textcolor="@color/color_9e9e9e" /> </linearLayout></linearLayout>

以上布局的效果图如下:

是不是差不多实现了微信一样的效果?那么我们怎么来判断以上布局是不是最优的呢?当然,我们是有工具来查看的。相信很多童鞋用过了,第一个就是 HIErarchy VIEw,第二个就是 显示GPU过度绘制。

HIErarchy VIEw检测布局嵌套层次

如果你是使用AS开发的话,你可以在 AS 工具栏中点击 ToolsC>AndroIDC>AndroID Device MonitorC>HIErarchy VIEw。(至于HIErarchy VIEw怎么使用这里就不仔细介绍了)你可以通过这个工具来查看当前布局的层次结构,如下图的布局的层次结构就是上面微信的布局:

ContentFrameLayout接点之后就是我们上面XML代码的布局了,从上图可以看到,我们布局最多有 5 层,其实你从代码中也可以看到是 5 层,那么我们是否能减少以上的布局的嵌套层次呢?答案是肯定的,废话不多说,我们直接上一份我优化过的布局代码吧。

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/color_eeeeee" tools:context=".MainActivity"> <TextVIEw  androID:ID="@+ID/tv_one"    androID:layout_margintop="20dp"  androID:background="@drawable/item_bg_select"  androID:drawableleft="@drawable/afe"  androID:text="@string/fIEnds" /> <androID.support.v7.Widget.linearLayoutCompat  androID:ID="@+ID/lv_two"    androID:layout_below="@+ID/tv_one"  app:divIDer="@drawable/lines"  app:divIDerpadding="10dp"  app:showdivIDers="mIDdle">  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/afg"   androID:text="@string/scan" />  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/afh"   androID:text="@string/shake" /> </androID.support.v7.Widget.linearLayoutCompat> <androID.support.v7.Widget.linearLayoutCompat  androID:ID="@+ID/lv_threed"    androID:layout_below="@+ID/lv_two"  app:divIDer="@drawable/lines"  app:divIDerpadding="10dp"  app:showdivIDers="mIDdle">  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/afd"   androID:text="@string/nearby" />  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/afb"   androID:text="@string/float_bottle" /> </androID.support.v7.Widget.linearLayoutCompat> <androID.support.v7.Widget.linearLayoutCompat    androID:layout_below="@+ID/lv_threed"  app:divIDer="@drawable/lines"  app:divIDerpadding="10dp"  app:showdivIDers="mIDdle">  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/agg"   androID:text="@string/shop@R_502_6817@" />  <TextVIEw      androID:background="@drawable/item_bg_select"   androID:drawableleft="@drawable/ak6"   androID:text="@string/games" /> </androID.support.v7.Widget.linearLayoutCompat> <include  layout="@layout/bottom_layout"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:layout_alignParentBottom="true" /></relativeLayout>

哇,代码量少了很多啊,代码也简洁了许多,让人看着就很舒服,那么我们到底进行了怎样的优化呢?从以下几点总结:

使用 style 主题来定义一个通用的属性,从而重复利用代码,减少代码量。上面代码使用了两个style,一个是textStyle 和 linerLayoutStyle ,代码如下:

<!--TextVIEw 的通用属性--> <style name="textStyle">  <item name="androID:layout_wIDth">match_parent</item>  <item name="androID:layout_height">wrap_content</item>  <item name="androID:textSize">16sp</item>  <item name="androID:textcolor">@androID:color/black</item>  <item name="androID:paddingRight">20dp</item>  <item name="androID:paddingleft">20dp</item>  <item name="androID:gravity">center_vertical</item>  <item name="androID:clickable">true</item> </style> <!--linerLayout 的通用属性--> <style name="linerLayoutStyle" >  <item name="androID:layout_wIDth">match_parent</item>  <item name="androID:layout_height">wrap_content</item>  <item name="androID:layout_margintop">20dp</item>  <item name="androID:background">@androID:color/white</item>  <item name="androID:orIEntation">vertical</item> </style>

2.减少布局嵌套的层次,上面布局使用TextVIEw可以设置四个方向图片来直接替代linerLayout下包裹一个ImageVIEw 和TextVIEw。从而这里减少了一层嵌套布局,再次利用relativeLayout相对布局又减少了一层桥套,提高了加载布局的效率。看图:

从图中看出,不仅减少了两层嵌套布局,而且组件数目也减少,从而减少布局绘制的时间,大大提高了布局加载效率。

3.使用 linearLayoutCompat 组件来实现线性布局元素之间的分割线,从而减少了使用VIEw来实现分割线效果。

linearLayoutCompat的具体内容请参考http://www.jb51.net/article/137253.htm

4.使用 include 标签加载底部菜单栏布局,include 标签的目的是重复利用布局,来减少代码了。

<?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="wrap_content" androID:background="@androID:color/white" androID:orIEntation="horizontal"> <TextVIEw  androID:layout_wIDth="0dp"  androID:layout_height="wrap_content"  androID:layout_weight="1"  androID:drawabletop="@drawable/ala"  androID:gravity="center"  androID:text="@string/weixin"  androID:textcolor="@color/color_9e9e9e"   /> <TextVIEw  androID:layout_wIDth="0dp"  androID:layout_height="wrap_content"  androID:layout_weight="1"  androID:drawabletop="@drawable/al9"  androID:gravity="center"  androID:text="@string/countans"  androID:textcolor="@color/color_9e9e9e" /> <TextVIEw  androID:layout_wIDth="0dp"  androID:layout_height="wrap_content"  androID:layout_weight="1"  androID:drawabletop="@drawable/alc"  androID:gravity="center"  androID:text="@string/finds"  androID:textcolor="@color/color_9e9e9e" /> <TextVIEw  androID:layout_wIDth="0dp"  androID:layout_height="wrap_content"  androID:layout_weight="1"  androID:drawabletop="@drawable/ale"  androID:gravity="center"  androID:text="@string/me"  androID:textcolor="@color/color_9e9e9e" /></linearLayout>

5.使用merge减少布局嵌套层次。

使用merge的前提条件就是merge标签必须是当前xml布局的根标签,例如:

<merge xmlns:androID="http://schemas.androID.com/apk/res/androID"> ........</merge> 

也就是merge标签必须是当前布局的父布局。一般merge标签和include结合使用来减少布局嵌套层次。例如有如下布局:两个button,以上一下。

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"> <relativeLayout  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <button   androID:ID="@+ID/button1"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="text1"/>  <button   androID:ID="@+ID/button2"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:layout_below="@+ID/button1"   androID:text="text2"/> </relativeLayout></relativeLayout>

该布局层次如下:

 

由上图看出除了根布局,我们自己写的布局有三层,两层都是relativeLayout。那么能否优化呢?

(1)首先我们可以利用include标签简化xml布局。结果变成如下:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"> <include layout="@layout/layout_item"></include></relativeLayout>

代码很清爽有木有,一目了然。然后layout_item布局如下:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"> <button  androID:ID="@+ID/button1"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="text1"/> <button  androID:ID="@+ID/button2"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_below="@+ID/button1"  androID:text="text2"/></relativeLayout>

到此,其实布局的嵌套层次和上面一样没有任何改变。那么我们利用merge标签来试试看结果怎样?merge标签代码如下:

<?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/button1"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="text1"/> <button  androID:ID="@+ID/button2"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_below="@+ID/button1"  androID:text="text2"/></merge>

此处仅仅把relativeLayout标签换成merge标签,效果却大有不同。

利用merge标签以后的布局层次如下:

 

很明显减少了一层relativeLayout布局,从而优化了布局。

总结:当父布局和子布局的根布局是同一种布局时,可以利用merge标签来减少一层嵌套布局。比如:你父布局是linerLayout,此时子布局也是linerLayout,就可以考虑使用merge来减少布局嵌套层次。

显示GPU过度绘制

你可以在手机打开 设置―->开发者选项―->显示GPU过度绘制,这个开关的作用是按不同颜色值来显示布局的过度绘制,绘制的层次从最优到最差:蓝,绿,淡红,红。给出一张直观的形象图片来表示吧

图片从上到下代表不同层次的OverDraw,我们在布局时候,尽量减少红色 Overdraw,看到更多的蓝色区域。来看看微信的 Overdraw图和我们自定义的布局Overdraw图吧

 

第一张图是 腾讯微信的Overdraw 图,第二张图片是我们自定义的 Overdraw 图片。从上面两张图片看出,我们自己的布局和微信原版的布局 Overdraw 过度绘制情况差不多,没啥区别。那么我们能不能去减少红色部分的过度绘制呢?试试吧!

我们先去掉最顶端布局relativeLayout的背景

androID:background="@color/color_eeeeee"

然后修改每个item选择资源 selector

androID:background="@drawable/item_bg_select1"

之前的 item_bg_select.xml 资源是如下代码:

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:drawable="@color/color_e0e0e0" androID:state_pressed="true"></item> <item androID:drawable="@androID:color/white"></item></selector>

修改之后的 item_bg_select1.xml资源代码如下:

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:drawable="@color/color_e0e0e0" androID:state_pressed="true"></item></selector>

我们发现,新的 selector资源去除了

<item androID:drawable="@androID:color/white"></item>

因为整个背景是白色的,无需重复设置正常情况下item的背景颜色。

修改之后的效果图如下:

看出,基本没有红色区域,从而提高布局的绘制效率。

总结:现在看来,我们通过减少背景颜色的设置来减少Overdraw的情况。我们自己布局过度绘制的情况比微信本身的情况有很大的改善,是不是感觉很nice~~。

懒加载布局 VIEwStub

除了以上两种方法来优化布局,还有其他办法来继续优化布局,在某些情况下,有些布局是仅在需要时才加载,比如小米手机的添加联系人功能就有在编辑姓名的时候有一个下拉按钮显示更多输入信息,看图

 

遇到这种情况,我们首先想到的 就是将不常用的元素使用INVISIBLE或者GONE进行隐藏,这样是否真的好呢?是否达到了 布局优化的最终效果呢?利用 INVISIBLE只是隐藏布局,但是布局还是占居当前位置,且系统在加载布局的时候这一部分还是会绘制出来,同样花费绘制时间。那么有没有好的办法来解决这一问题呢?不言而喻,我们可以使用懒加载布局 VIEwStub。

VIEwStub是AndroID为此提供了一种非常轻量级的控件。VIEwStub虽说也是VIEw的一种,但是它没有大小,没有绘制功能,也不参与布局,资源消耗非常低,将它放置在布局当中基本可以认为是完全不会影响性能的。

下面我们来学习一下 VIEwStub的使用方法吧!

<?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" androID:padding="20dp"> <EditText  androID:ID="@+ID/et_name"  androID:layout_wIDth="150dp"  androID:drawableRight="@drawable/a0e"  androID:layout_height="wrap_content"  androID:hint="@string/name" /> <VIEwStub  androID:ID="@+ID/vIEw_stub"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout="@layout/item_name" /> <EditText  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/comp" /> <EditText  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/lead" /></linearLayout>

item_name.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"> <EditText  androID:ID="@+ID/et_name1"  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/name1" /> <EditText  androID:ID="@+ID/et_name2"  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/name2" /> <EditText  androID:ID="@+ID/et_name3"  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/name3" /> <EditText  androID:ID="@+ID/et_name4"  androID:layout_wIDth="150dp"  androID:layout_height="wrap_content"  androID:hint="@string/name4" /></linearLayout>

然后你在代码中这么使用即可

findVIEwByID(R.ID.et_name).setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw v) {    VIEwStub vIEwStub = (VIEwStub) findVIEwByID(R.ID.vIEw_stub);    if (null != vIEwStub) {     //主要是这一句显示更多布局     VIEw vIEw = vIEwStub.inflate();     EditText name1 = (EditText) vIEw.findVIEwByID(R.ID.et_name1);     EditText name2 = (EditText) vIEw.findVIEwByID(R.ID.et_name2);     EditText name3 = (EditText) vIEw.findVIEwByID(R.ID.et_name3);     EditText name4 = (EditText) vIEw.findVIEwByID(R.ID.et_name4);    }   }  });

效果图如下:

 

从效果图可以看出,当用户点击姓名下拉按钮时,其他关于姓名的布局就加载出来了,而且原来的布局是显示在VIEwStub布局之下的,位置显示没有任何异常。当然你可以通过 setVisibility(VIEw.VISIBLE)或者vIEwStub.inflate()方来来让其显示,通过setVisibility(VIEw.INVISIBLE)来隐藏 VIEwStub。

AndroID lint 工具

这里不展开介绍 lint 工具,感兴趣的童鞋可以自己网上搜索一把,这个工具主要是用来检查工程中代码的不合理,布局不合理,资源重复,图片重复,等,让开发者进一步优化自己的应用。如果你是AS 用户,你可以在工具栏 Analyze―>Inspect Code 打开此工具使用。

您可能感兴趣的文章:Android中使用ViewStub实现布局优化Android布局优化之ViewStub控件Android中ListView Item布局优化技巧 总结

以上是内存溢出为你收集整理的详解Android布局优化全部内容,希望文章能够帮你解决详解Android布局优化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1142825.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存