美文网首页
布局中<merge>、<viewstub>

布局中<merge>、<viewstub>

作者: PeterHe888 | 来源:发表于2018-05-15 19:00 被阅读100次

1.<include/>

<include/>标签作用重用布局

  • include标签可以使用单独的layout属性
  • include标签指定了id,同时layout也定义了id,则layout的id会被覆盖
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"   
    android:layout_width=”match_parent”  
    android:layout_height=”match_parent”  
    android:background="@color/app_bg"  
    android:gravity="center_horizontal">  
  
    <include layout="@layout/titlebar"/>  
  
    <TextView android:layout_width=”match_parent”  
              android:layout_height="wrap_content"  
              android:text="@string/hello"  
              android:padding="10dp" />  
  
    ...  
  
</LinearLayout>  

2.<merge/>

减少视图层级的复用,<merge/>标签在UI结构化优化起着非常重要的作用,可以减少多余的层级,优化UI。
使用:

  • 用于替换FrameLayout
  • 当一个布局包含另一个布局时,如一个垂直的线性布局,引入了一个垂直的include,使用merge可以消除include布局中的线性布局。
<merge xmlns:android="http://schemas.android.com/apk/res/android">  
  
    <Button  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
        android:text="@string/add"/>  
  
    <Button  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
        android:text="@string/delete"/>  
  
</merge>  

3.<viewstub>
需要时加载,优化初始化UI性能,如不常用的进度条、错误消息等使用viewstub,减少内存使用量,加快布局渲染。它是一个不可见,大小为0的view。

<ViewStub  
    android:id="@+id/stub_import"  
    android:inflatedId="@+id/panel_import"  
    android:layout="@layout/progress_overlay"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" />  

使用时加载:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
// or  
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 

当调用inflate函数的时候,viewstub被引用的资源替代,并返回引用的view,这样程序可以直接得到view的引用而不用再次调用findViewById来查找
注:viewstub目前不支持merge标签。

相关文章

  • 布局中<merge>、<viewstub>

    1. 标签作用重用布局 include标签可以使用单独的layout属性 include标签指定了id,同时lay...

  • Read a story

    This is a lion. lt's big. lt's strong. lt has big teeth. ...

  • Mybatis中特殊符号转移

    1. 写法1 原符号替换符号<<<=<=>>>=>=<><>&&'&a...

  • Read a story

    lt's hot?? lt's cool here. lt's a hat. What's this? Do yo...

  • test

    <script>alert(1);</script>

  • 无标题文章

    <script>alert('hello’);</script>

  • 芯科通信招聘fw固件工程师

    职责描述 工作职责:lt;/pgt;lt;pgt;1、负责光模块firmware的软件设计、维护以及优化;lt;/...

  • 打卡第47天

    Attention is vitality. lt connects you with others. lt ma...

  • Listen and enjoy

    lt can be bitter. lt can be sweet. Sometimes it's black. ...

  • 2018-10-08

    线性系统的LT分析法一、数学角度1、对微分方程进行LT分析eg:同求LT变换得到2、对电路进行LT处理对于线性电路...

网友评论

      本文标题:布局中<merge>、<viewstub>

      本文链接:https://www.haomeiwen.com/subject/cseydftx.html