Android抽象布局——include、merge 、ViewStub

来源:互联网 发布:北京代办韩国签证 知乎 编辑:程序博客网 时间:2024/05/20 21:48
在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。


1、布局重用<include />

<include />标签能够重用布局文件,简单的使用如下:


[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"   
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     android:background="@color/app_bg"  
  6.     android:gravity="center_horizontal">  
  7.   
  8.     <include layout="@layout/titlebar"/>  
  9.   
  10.     <TextView android:layout_width=”match_parent”  
  11.               android:layout_height="wrap_content"  
  12.               android:text="@string/hello"  
  13.               android:padding="10dp" />  
  14.   
  15.     ...  
  16.   
  17. </LinearLayout>  

    1)<include />标签可以使用单独的layout属性,这个也是必须使用的。

    2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layoutID会被覆盖,解决方案。

    3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_widthlayout_height两个属性

    4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决(参考):

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);   
  2.   
  3. bookmarks_container_2.findViewById(R.id.bookmarks_list);  


2、减少视图层级<merge />

    <merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <Button  
  4.         android:layout_width="fill_parent"   
  5.         android:layout_height="wrap_content"  
  6.         android:text="@string/add"/>  
  7.   
  8.     <Button  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/delete"/>  
  12.   
  13. </merge>  

     现在,当你添加该布局文件时(使用<include />标签),系统忽略<merge />节点并且直接添加两个Button。更多<merge />介绍可以参考Android Layout Tricks #3: Optimize by merging


3、需要时使用<ViewStub />

    <ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0View。<ViewStub />标签使用如下:


[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <ViewStub  
  2.     android:id="@+id/stub_import"  
  3.     android:inflatedId="@+id/panel_import"  
  4.     android:layout="@layout/progress_overlay"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:layout_gravity="bottom" />  


当你想加载布局时,可以使用下面其中一种方法:


[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
  2. // or  
  3. View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  


当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
ViewStub目前有个缺陷就是还不支持 <merge /> 标签。

include和merge标记的作用主要是为了解决layout的重用问题。比如我们有三四个Activity但是他们都要用到同一个样式的标题栏,虽然我们把一样的代码copy个三四遍也没关系,但实在是太丑了,而且效率太低,如果这个标题栏要改样式,你岂不是要去三四个地方分别改动。为了解决这个问题,android中有了include和merge标记 以下为标题栏的layout文件titlebar.xml  我们将使用Include标记重用这个文件<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width=”match_parent”    android:layout_height="wrap_content"    android:background="@color/titlebar_bg">    <ImageView android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:src="@drawable/gafricalogo" /></FrameLayout>那么在那三四个activity中你可以适用Include标记<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width=”match_parent”    android:layout_height=”match_parent”    android:gravity="center_horizontal">    <include layout="@layout/titlebar"/>      <TextView android:layout_width=”match_parent”              android:layout_height="wrap_content"              android:text="@string/hello" />    ...</LinearLayout>调用了Include之后,titlebar文件的内容就被完全嵌入到了include所指定的位置。而且你还可以在include中重新更改一些属性的值,比如<include android:id=”@+id/news_title”         android:layout_width=”match_parent”         android:layout_height=”match_parent”         layout="@layout/title"/>原来layout中的wrap_content属性就被改成了match_parent属性再来说一下merge标记上面的include有一个副作用就是他多套了一层root节点FrameLayout ,使得再构图的时候会多花费一点时间如果你不能容忍这个的话那你可以试一下merge标记titlebar2.xml<merge xmlns:android="http://schemas.android.com/apk/res/android">    <ImageView android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:src="@drawable/gafricalogo" /></merge>这样行成的titlebar2文件就少了外层的root节点,merge标记可以直接成为root节点,当titlebar2被include到文件中时,merge标记就会被忽略掉,而直接由里面的ImageView取代原来include的位置。避免了冗余的layout。 所以include和merge是配合使用的,不是一个互斥的或者说是平级的关系。再来说一个在使用这两个标签时最容易出现的问题。经常会有同学在RelativeLayout中使用include标签但是却发现include进来的控件无法用layout_alignParentBottom="true"之类的标签来调整。这个真的非常恼火。其实解决方法非常简单,只要你在include的时候同时重载下layout_width和layout_height这两个标签就可以了。如果不重载,任何针对include的layout调整都是无效的!

0 0
原创粉丝点击