布局优化

2017-07-18  本文已影响28人  贝贝ovo

一.核心思想

减少布局文件的层次

布局的层次少了,意味着Android绘制时的工作量少了,程序的性能自然就提高了。

二.方法

1.删除布局中无用的控件和层级
2.有选择的使用ViewGroup
3.采用<include>标签,<merge>标签,ViewStub

详解
1.删除布局中无用的控件和层级 不详解
2.有选择的使用ViewGroup

如果布局中既可以用LinearLayout也可以用RelativeLayout,优先采用LinearLayout。(RelativeLayout功能比较复杂,布局过程需要花费更多的CPU时间;FrameLayoutLinearLayout 一样都是一种简单高效的ViewGroup

3.采用<include>标签,<merge>标签,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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

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

@layout/include_text指定了另一个布局,这种方式可以复用这个布局,比如常用的titlebar

注:

  1. <include>标签只支持android:id 跟 以android:layout_开头的属性
  2. <include>标签指定的属性同时被包含的布局文件也指定了,以<include>标签为准 (在被包含的布局文件设置的background,会以include和被包含的布局文件中设定的两者最小面积为准)

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Hello World!"
        />
</merge>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

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

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/ll_root"
        android:layout="@layout/view_stub"
        >
    </ViewStub>
</LinearLayout>

android:layout="@layout/view_stub"其中@layout/view_stub为按需加载的布局,注意跟<include>标签中的 layout="@layout/include_text" 属性引用不一样
android:inflatedId="@+id/ll_root" 其中@+id/ll_root为按需加载布局文件中的根元素id

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Hello World!"
        />
</LinearLayout>

如何按需加载?两种方式
findViewById(R.id.view_stub).setVisibility(View.VISIBLE);
或者
((ViewStub)findViewById(R.id.view_stub)).inflate();
当加载后,ViewStub就会被它内部的布局替换掉,这时候ViewStub就不再是整个布局结构中的一部分了。另 ViewStub不支持<merge>标签。

参考任玉刚的Android开发艺术探索

上一篇 下一篇

猜你喜欢

热点阅读