Merge和ViewStub布局标签

以下内容整理自互联网,仅用于个人学习


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

一个宽高都为0的view,默认不可见,只有通过调用setVisibility设置为可见或者调用了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();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容