Android 布局优化 include,merge,viewstub标签
2017-08-09 09:31
671 查看
布局优化在xml文件中用到了三个标签 include,merge,viewstub。
1.include用的很常见,就是布局代码的复用,先定义一个被复用的布局文件,然后在需要的地方引用过来就行了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lee="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/view_title_bar" />
</LinearLayout>
2.然后是merge,减少视图的节点数,剪短视图绘制的时间,从而提高性能。特点如下:
merge必须放在布局文件的根节点上。
merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点。
因为merge不是View,所以对merge标签设置的所有属性都是无效的
对于merge的使用,后面会用一个自定义组合控件的方式来给出。
3.viewstub:(引用别人的)
Activity布局文件定义my_sub_activity.xml:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
其中Android:inflatedId指定了懒加载视图跟节点的ID。android:layout指定了懒加载的视图。android:layout_width和android:layout_height分别指定了懒加载视图的宽和高。
懒加载布局文件my_sub_tree.xml:
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
懒加载视图里只有一个TextView(这里只是做测试,正常情况下这里应该是一个复杂的视图)。
ViewStubActivity的代码:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
代码里设置了布局,并在点击后查到到ViewStub对象,并加载视图。
下面看看加载视图前后的对比图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/01/58dd533c75493150f9f93d2976e226fe)
1.include用的很常见,就是布局代码的复用,先定义一个被复用的布局文件,然后在需要的地方引用过来就行了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lee="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/view_title_bar" />
</LinearLayout>
2.然后是merge,减少视图的节点数,剪短视图绘制的时间,从而提高性能。特点如下:
merge必须放在布局文件的根节点上。
merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
自定义View如果继承LinearLayout,建议让自定义View的布局文件根节点设置成merge,这样能少一层结点。
因为merge不是View,所以对merge标签设置的所有属性都是无效的
对于merge的使用,后面会用一个自定义组合控件的方式来给出。
3.viewstub:(引用别人的)
特性:
1. ViewStub是一个继承了View类的视图。 2. ViewStub是不可见的,实际上是把宽高都设置为0 3. 可以通过布局文件的android:inflatedId或者调用ViewStub的setInflatedId方法为懒加载视图的跟节点设置ID 4. ViewStub视图在首次调用setVisibility或者inflate方法之前,一直存在于视图树中 5. 只需要调用ViewStub的setVisibility或者inflate方法即可显示懒加载的视图 6. 调用setVisibility或者inflate方法之后,懒加载的视图会把ViewStub从父节点中替换掉 7. ViewStub的inflate只能被调用一次,第二次调用会抛出异常,setVisibility可以被调用多次,但不建议这么做(后面说原因) 8. 为ViewStub赋值的android:layout_属性会替换待加载布局文件的根节点对应的属性 9. inflate方法会返回待加载视图的根节点
使用:
我在一个activity上放置了一个按钮,点击后加载懒加载的视图。Activity布局文件定义my_sub_activity.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"> <Button android:onClick="onClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载视图"/> <ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree" android:layout="@layout/my_sub_tree" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
其中Android:inflatedId指定了懒加载视图跟节点的ID。android:layout指定了懒加载的视图。android:layout_width和android:layout_height分别指定了懒加载视图的宽和高。
懒加载布局文件my_sub_tree.xml:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background= 4000 "#ffffff" android:gravity="center" android:padding="10dip" android:text="懒加载视图" android:textColor="#000000" android:textSize="22sp"> </TextView>1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
懒加载视图里只有一个TextView(这里只是做测试,正常情况下这里应该是一个复杂的视图)。
ViewStubActivity的代码:
public class ViewStubActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_sub_activity); } @Override public void onClick(View v) { // 这里调用的是inflate方法,当然,也可以调用setVisibility方法(但是不建议这么做) // 只能点击一次加载视图按钮,因为inflate只能被调用一次 // 如果再次点击按钮,会抛出异常"ViewStub must have a non-null ViewGroup viewParent" ((ViewStub) findViewById(R.id.stub)).inflate(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
代码里设置了布局,并在点击后查到到ViewStub对象,并加载视图。
下面看看加载视图前后的对比图:
相关文章推荐
- android 布局优化标签<include/>、<merge />、<ViewStub />
- Android最佳性能实践——布局优化技巧之<include>、<merge>标签及仅在需要时才加载布局的:ViewStub
- Android布局优化几个重要标签的使用include、merge、ViewStub、requestFocus
- Android布局优化——include、merge和ViewStub标签
- Android布局优化:include 、merge、ViewStub的详细总结
- Android-布局优化(include , merge , ViewStub)
- Android布局优化之ViewStub、include、merge使用与源码分析
- Android布局优化技巧--<include>/<merge>/<viewStub>
- android开发布局优化—include、merge、viewstub源码分析总结
- Android布局优化之ViewStub、include、merge使用与源码分析
- Android布局优化之Merge Include ViewStub使用与源码分析
- Android 布局优化之include与merge,最后有ViewStub
- Android 布局优化之include、merge、ViewStub
- Android布局优化之<merge>与<ViewStub>标签使用
- Android布局优化之ViewStub、include、merge使用与源码分析
- 布局优化神器 include 、merge、ViewStub标签详解
- include、ViewStub、merge优化布局标签
- Android 布局优化之——include、merge 、ViewStub
- 【Android布局】布局优化之ViewStub、include、merge使用与源码分析
- Android布局优化之Merge、Include、ViewStub使用与源码分析