1 Merge
Merge作为A布局根标签,其他布局文件B通过include引用A时,Merge标签会被去掉,在include里存放的是merge的子标签,以此减少布局文件的层次。
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="merge标签使用" />
</merge>
如上面的布局,外层的merge会在最终的布局中去掉。
2 ViewStub
轻量级View,它看不见,且不占布局空间。占用资源非常小。只有当ViewStub被设置为可见时或者调用了ViewStub.inflate()时ViewStub所指向的布局文件才会被inflate和实例化,然后ViewStub布局属性全部传给它所指向的布局。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<ViewStub
android:id="@+id/viewstub_demo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:layout="@layout/viewstub_demo_text_layout"/>
</LinearLayout>
在onCreate函数中:
ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);
stub.inflate();
使用ViewStub的一些注意点:
- ViewStub中的layout布局不能使用merge标签,否则会报错;
- ViewStub的inflate只能执行一次,显示了之后,就不能再使用ViewStub控制它了。