您的位置:首页 > 移动开发 > Android开发

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:(引用别人的)

特性:

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开发