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

1、布局重用

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

[html]view plaincopyprint?

android:orientation="vertical"

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:background="@color/app_bg"

android:gravity="center_horizontal">

android:layout_height="wrap_content"

android:text="@string/hello"

android:padding="10dp"/>

...

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

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

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

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

[html]view plaincopyprint?

Viewbookmarks_container_2=findViewById(R.id.bookmarks_favourite);

bookmarks_container_2.findViewById(R.id.bookmarks_list);

2、减少视图层级

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

[html]view plaincopyprint?

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/add"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/delete"/>

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

3、需要时使用

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

[html]view plaincopyprint?

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"/>

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

[java]view plaincopyprint?

((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目前有个缺陷就是还不支持标签。


参考:http://blog.csdn.net/xyz_lmn/article/details/14524567

http://blog.csdn.net/chenlaic/article/details/6090069

http://www.tuicool.com/articles/jyyUV33

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. 箭头函数的基础使用
  3. NPM 和webpack 的基础使用
  4. Python list sort方法的具体使用
  5. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  6. android 按钮设计中state_selected属性
  7. Android(安卓)Studio系列教程三--快捷键
  8. 【Android开发基础】应用界面主题Theme使用方法
  9. android 出现gen already exists but is not a source folder错

随机推荐

  1. Android随笔之——闹钟制作铺垫之AlarmMa
  2. Android-Fresco系列2 加载资源
  3. Android(安卓)Kotlin 学习总结(一) 《KAE
  4. SeekBar涓?labelFor鏍囩no resource id
  5. Android(安卓)Cordova 插件开发之自定义
  6. EACCES (permission denied)解决办法 and
  7. android 鑾峰彇灞忓箷鏂瑰悜璇﹁В
  8. AndroidAZ系列:四大组件之Broadcast(All,
  9. android http通过post上传文件和提交参数
  10. 【转】打开eclipse点更新后,出现This Andr