我的Android之路android开发Android

android性能优化-渲染-嵌套标签

2015-08-08  本文已影响1729人  DanielHan

1.重用布局include

include标签常用于将布局中的公共部分提取出来供其他layout共用,以实现布局模块化,这在布局编写方便提供了大大的便利。例如,每个activity都会用到的titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
</FrameLayout>

用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/hello"
              android:padding="10dp" />

    ...

</LinearLayout>
注: 可以在include标签中覆盖被引用的根布局的android:layout_*属性,但是前提是android:layout_height和android:layout_width都要写,如:
<include  android:id=”@+id/news_title” 
          android:layout_width=”match_parent”
          android:layout_height=”match_parent”         
          layout=”@layout/title”/>

2.减少布局层次merge

merge标签可用于两种典型情况:

  1. 布局顶结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity内容视图的parent view就是个FrameLayout,所以可以用merge消除只剩一个。
  2. 某布局作为子布局被其他布局include时,使用merge当作该布局的顶节点,这样在被引入时顶结点会自动被忽略,而将其子节点全部合并到主布局中。
    用hierarchy viewer查看main.xml布局如下图
1.png

如图,红色部分的RelativeLayout是多余的,解决如下:

<?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/button"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:layout_above="@+id/text"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:layout_alignParentBottom="true"
        android:text="@string/app_name" />

</merge>

结果如下图:

2.png
注:listview的item布局根目录不能用merge,否则会报错:

merge can be used only with a valid ViewGroup root and attachToRoot=true

3.需要时加载ViewStub

viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。
viewstub常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等。
定义如下:

<ViewStub
    android:id="@+id/stub_import"
    android:inflatedId="@+id/panel_import"
    android:layout="@layout/stub_import"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

加载ViewStub:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

一旦visible/inflated,ViewStub元素将被layout属性值替换,并且android:inflatedId的值将作为根布局的id

注:
1. ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
2. 要想获得stub_import中的控件,用如下代码
  //必须使用inflate方法,否则下面获取不到子控件
  View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
  importPanel.findViewById(R.id.children);

另外欢迎关注我的:
Github
微博
掘金
微信公众号

蛋妞码农
上一篇 下一篇

猜你喜欢

热点阅读