🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### 15.1.1 布局优化 布局优化的思想很简单,就是尽量减少布局文件的层级,这个道理是很浅显的,布局中的层级少了,这就意味着Android绘制时的工作量少了,那么程序的性能自然就高了。 如何进行布局优化呢?首先删除布局中无用的控件和层级,其次有选择地使用性能较低的ViewGroup,比如RelativeLayout。如果布局中既可以使用LinearLayout也可以使用RelativeLayout,那么就采用LinearLayout,这是因为RelativeLayout的功能比较复杂,它的布局过程需要花费更多的CPU时间。FrameLayout和LinearLayout一样都是一种简单高效的ViewGroup,因此可以考虑使用它们,但是很多时候单纯通过一个LinearLayout或者FrameLayout无法实现产品效果,需要通过嵌套的方式来完成。这种情况下还是建议采用RelativeLayout,因为ViewGroup的嵌套就相当于增加了布局的层级,同样会降低程序的性能。 布局优化的另外一种手段是采用`<include>`标签、`<merge>`标签和ViewStub。`<include>`标签主要用于布局重用,`<merge>`标签一般和`<include>`配合使用,它可以降低减少布局的层级,而ViewStub则提供了按需加载的功能,当需要时才会将ViewStub中的布局加载到内存,这提高了程序的初始化效率,下面分别介绍它们的使用方法。 `<include>`标签 `<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/text" android:padding="5dp" /> ... </LinearLayout> 上面的代码中,@layout/titlebar指定了另外一个布局文件,通过这种方式就不用把titlebar这个布局文件的内容再重复写一遍了,这就是`<include>`的好处。`<include>`标签只支持以android:layout_开头的属性,比如android:layout_width、android:layout_height,其他属性是不支持的,比如android:background。当然,android:id这个属性是个特例,如果<include>指定了这个id属性,同时被包含的布局文件的根元素也指定了id属性,那么以<include>指定的id属性为准。需要注意的是,如果<include>标签指定了android:layout_*这种属性,那么要求android:layout_width和android:layout_height必须存在,否则其他android:layout_*形式的属性无法生效,下面是一个指定了android:layout_*属性的示例。 <include android:id="@+id/new_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/> `<merge>`标签 `<merge>`标签一般和`<include>`标签一起使用从而减少布局的层级。在上面的示例中,由于当前布局是一个竖直方向的LinearLayout,这个时候如果被包含的布局文件中也采用了竖直方向的LinearLayout,那么显然被包含的布局文件中的LinearLayout是多余的,通过`<merge>`标签就可以去掉多余的那一层LinearLayout,如下所示。 <merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/one"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/two"/> </merge> `ViewStub ` ViewStub继承了View,它非常轻量级且宽/高都是0,因此它本身不参与任何的布局和绘制过程。ViewStub的意义在于按需加载所需的布局文件,在实际开发中,有很多布局文件在正常情况下不会显示,比如网络异常时的界面,这个时候就没有必要在整个界面初始化的时候将其加载进来,通过ViewStub就可以做到在使用的时候再加载,提高了程序初始化时的性能。下面是一个ViewStub的示例: <ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/layout_network_error" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> 其中stub_import是ViewStub的id,而panel_import是layout/layout_network_error这个布局的根元素的id。如何做到按需加载呢?在需要加载ViewStub中的布局时,可以按照如下两种方式进行: ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); 或者 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 当ViewStub通过setVisibility或者inflate方法加载后,ViewStub就会被它内部的布局替换掉,这个时候ViewStub就不再是整个布局结构中的一部分了。另外,目前ViewStub还不支持<merge>标签。