Android之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

轻量级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控制它了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值