Android布局优化:include、merge、viewstub使用区别

Android布局优化:includemergeviewstub使用区别

include标签的使用:

文件1:titlebar.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="返回按钮" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="提示文字"
        android:textSize="20sp" />

    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="关闭按钮" />

</RelativeLayout>

文件2:activity_main.xml

<RelativeLayout 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"" >
	<!--引入titlebar页面-->
    <include
        android:layout_width="match_parent"
        android:layout_height="40dp"
        layout="@layout/titlebar" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="click"
        android:text="按钮显示 />
</RelativeLayout>

include标签使用注意点:

  1. <include>标签当中,可以重写所有layout属性的,如上面include中指定的layout属性将会覆盖掉titlebar中指定的layout属性。
    而非layout属性则无法在<include>标签当中进行覆写。另外需要注意的是,如果我们想要在<include>标签当中覆写layout属性,
    必须要将layout_widthlayout_height这两个属性也进行覆写,否则覆写效果将不会生效
  2. 一个xml布局文件有多个include标签需要设置ID,才能找到相应子View的控件,否则只能找到第一个includelayout布局,以及该布局的控件。
  3. 如果我们给include所加载的layout布局的根容器设置了id属性,也在include标签中设置了id属性,同时需要在代码中获取根容器的控件对象
    时,最好将这两个id设置相同的名称!否则,可能获取不到根容器对象,即为null

二 merge标签的使用

文件1:login.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
     <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="登录按钮" />
</merge>

文件2:activity_login.xml

<LinearLayout 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"
    android:orientation="vertical"
    android:background="@android:color/holo_blue_light">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:hint="请输入用户名" />

    <include layout="@layout/login" />

</LinearLayout>

通过使用merge标签,会比第一种的使用include标签减少一层viewgroup布局容器,提升UI渲染性能.

merge标签使用注意点:

  1. 根布局是FrameLayout且不需要设置backgroundpadding等属性,可以用merge代替,因为ActivityContentView父元素就是FrameLayout,所以可以用merge消除只剩一个.
  2. 因为merge标签并不是View,所以在通过LayoutInflate.inflate()方法渲染的时候,第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点.由于merge不是View所以对merge标签设置的所有属性都是无效的.
  3. merge标签必须使用在根布局,并且ViewStub标签中的layout布局不能使用merge标签.

viewstub标签的使用

文件1:otherinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/weichat_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="请输入微信号" />

    <EditText
        android:id="@+id/address_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:hint="请输入家庭住址" />

</LinearLayout>

文件2:activity_main.xml

<LinearLayout 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"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:hint="请输入用户名" />

    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/otherinfo" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="show"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:text="显示" />

</LinearLayout>

文件3:activity中代码如下

	public void show(){
        ViewStub stub = ((ViewStub) findViewById(R.id.viewstub));
        if(stub!=null){
            View stubView = stub.inflate();		//inflate方法只能调用一次,第二次会抛出异常
            EditText address = (EditText) stubView.findViewById(R.id.address_id);
            EditText wechatId = (EditText) stubView.findViewById(R.id.weichat_id);
        }
    }

ViewStub标签使用注意点:

  1. ViewStub标签不支持merge标签。因此这有可能导致加载出来的布局存在着多余的嵌套结构,具体如何去取舍就要根据各自的实际情况来决定了。
  2. ViewStubinflate只能被调用一次,第二次调用会抛出异常。
  3. 虽然ViewStub是不占用任何空间的,但是每个布局都必须要指定layout_widthlayout_height属性,否则运行就会报错。

关于ViewStub主要属性以及方法说明如下:

  • android:layout属性
    加载包含的layout布局文件;
  • android:inflatedId属性
    重写包含的layout布局文件的根容器id
  • inflate()方法
    setVisible(int)方法作用类似,都可以使内容得以显示,只是inflate()会返回一个View对象,避免了额外使用findViewById()方法获取layout视图对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值