美文网首页
Android布局优化

Android布局优化

作者: weiyushuai | 来源:发表于2016-05-16 11:17 被阅读177次

阅读Android开发艺术探索随笔记之布局优化


布局优化就是尽量减少布局文件的层级,布局中层级少了,这就意味着Android绘制时工作量少了.那么程序的性能自然也就提高了.

1,如何进行布局优化呢 ? 首先删除布局中无用的控件和层级.其次使用有选择地使用性能较低的ViewGroup.比如Relativelayout.如果布局中既可以使用Linearlayout也可以使用Relativelayout.那么就采用Linearlayout.这是因为Relativelayout的功能比较复杂.它的布局过程需要花费更多的CPU时间.Framelayout和Linearlayout一样都是一种简单高效的ViewGroup.因此也可以考虑使用它们.但是很多时候单纯通过一个LinearLayout或者FrameLayout无法实现产品效果.需要通过嵌套的方式来完成.这种情况下还是建议采用RelativeLayout.因为ViewGroup的嵌套就相当于增加了布局的层级.同样会降低程序的性能.

2,布局优化的另外一种手段是采用<include>标签和ViewStub.<include>标签主要用于布局重用,<merge>标签一般和<include>配合使用.它可以降低煎炒布局的层级.而ViewStub则提供了按需加载的功能.当需要时才会将ViewStub中的布局加载到内存.这提高了程序的初始化效率.下面分别介绍他们的使用方法.

<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/activity_base_title_bar"/>       
       <TextView        
          android:layout_width="match_parent"            
          android:layout_height="wrap_content"       
          android:padding="5dp"/>    
</LinearLayout>

上面的代码中,@layout/activity_base_title_bar指定了另外一个布局文件.通过这种方式就不用把title_bar这个布局文件的内容在重复写一遍了.这就是<include>的好处.<include>标签只支持android:layout_开头的属性.比如android:layout_width,android:latout_height,其他属性是不支持的.比如android:background.当然android:id这个属性是个特例如果<include>指定了这个id属性.同时被包含的布局文件的根元素也指定了id属性.那么以android:layout_这种属性,那么要求android:layout_width和android:layout_height必须存在,否则其他android:layout_形式的属性无法生效.下面是一个指定了android:layout_*属性实例

     <include
        android:id="@+id/titlebar"
        layout="@layout/activity_base_title_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

<merge>标签

<merge>标签一般和<include>标签一起使用从而减少布局的层级.在上面的实例中由于当前布局是一个竖直方向的LinearLayout这个时候如果被包含的布局文件中采用了竖直方向的linearlayout那个显示被包含的布局文件中的Linearlayout是多余的通过<merge>标签就可以去掉多余的一层LinearLayout如下所示:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />

</merge>

下面说下ViewStub

ViewStub继承了View.它非常轻量级且宽/高都是0.因此它本身不参与任何的布局和绘制过程.ViewStub的意义在于按需加载所需的布局文件.在实际开发中.有很多布局文件在正常情况下不会显示比如网络异常时界面.这个时候就没用必要在整个界面初始化的时候将其加载进来通过ViewStub就可以做到在使用的时候在加载提高了程序初始化时的性能.

<ViewStub
        android:id="@+id/network_error"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/network_error"
         />

使用方式

View network_error=((ViewStub)findViewById(R.id.network_error)).setVisibility(View.VISIBLE);
或者
View network_error=((ViewStub)findViewById(R.id.network_error)).inflate();

当ViewStub通过setVisibility或者inflate方法加载后.ViewStub就会被它内部的布局替换掉这个时候ViewStub就不再是布局结构中的一部分了.另外 目前ViewStub还不支持<merge>标签

相关文章

网友评论

      本文标题:Android布局优化

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