装载:张兴业  http://blog.csdn.net/xyz_lmn

      在布局优化中,Androi的官方提到了这三种布局,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。


1、布局重用

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


[html]  view plain  copy  print ?
  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)标签可以使用单独的layout属性,这个也是必须使用的。

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

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

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

[html]  view plain  copy  print ?
  1. View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);   
  2.   
  3. bookmarks_container_2.findViewById(R.id.bookmarks_list);  


2、减少视图层级

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

[html]  view plain  copy  print ?
  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>  

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


3、需要时使用

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


[html]  view plain  copy  print ?
  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 plain  copy  print ?
  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目前有个缺陷就是还不支持  标签。


更多标签介绍可以参考《Android Layout Tricks #3: Optimize with stubs》


更多相关文章

  1. Android(安卓)UI开发第四十一篇——墨迹天气3.0引导界面及动画实
  2. Android(安卓)样式的开发
  3. 2.2.3帧布局
  4. Android线性布局与表格布局
  5. Android软键盘弹出将底部栏顶上去并不会挤压界面
  6. Android(安卓)切换卡(TabWidget)
  7. Android虚拟导航栏遮挡底部的输入框的解决方法
  8. 详解Android中获取SD卡和内存的空间信息
  9. [置顶] Android(安卓)中轴时光轴

随机推荐

  1. boost项目复盘(二)
  2. 初试kafka监控及管理利器之kafka-eagle
  3. ansible安装碰到的问题
  4. kubernetes中启动探针startupProbe
  5. ansible初入
  6. 《Golang从入门到跑路》之指针
  7. idea2020.3.2 没有javaweb选项
  8. 《Golang从入门到跑路》之map的初识
  9. 一次
  10. SQL中的ALL、ANY和SOME的用法介绍