Android布局优化——抽象布局:include、merge 、ViewStub
来源:互联网 发布:ubuntu net snmp 安装 编辑:程序博客网 时间:2024/05/09 01:11
在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。
1、布局重用<include />
<include />标签能够重用布局文件,简单的使用如下:
01
<
font
color
=
"#000"
><
font
face
=
"Arial"
><
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
02
android:orientation
=
"vertical"
03
android:layout_width=”match_parent”
04
android:layout_height=”match_parent”
05
android:background
=
"@color/app_bg"
06
android:gravity
=
"center_horizontal"
>
07
08
<
include
layout
=
"@layout/titlebar"
/>
09
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
> </
font
></
font
>
1)<include />标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖,解决方案。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决:
1
<font color=
"#000"
><font face=
"Arial"
>View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);
2
3
bookmarks_container_2.findViewById(R.id.bookmarks_list); </font></font>
<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。
01
<
font
color
=
"#000"
><
font
face
=
"Arial"
><
merge
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
02
03
<
Button
04
android:layout_width
=
"fill_parent"
05
android:layout_height
=
"wrap_content"
06
android:text
=
"@string/add"
/>
07
08
<
Button
09
android:layout_width
=
"fill_parent"
10
android:layout_height
=
"wrap_content"
11
android:text
=
"@string/delete"
/>
12
13
</
merge
> </
font
></
font
>
现在,当你添加该布局文件时(使用<include />标签),系统忽略<merge />节点并且直接添加两个Button。
3、需要时使用<ViewStub />
<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0的View。<ViewStub />标签使用如下:
1
<
font
color
=
"#000"
><
font
face
=
"Arial"
><
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"
/> </
font
></
font
>
当你想加载布局时,可以使用下面其中一种方法:
1
<font color=
"#000"
><font face=
"Arial"
>((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
2
// or
3
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); </font></font>
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
- Android布局优化——抽象布局:include、merge 、ViewStub
- 【Android】抽象布局 — include、merge 、ViewStub
- 抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub的使用
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- Android抽象布局——include、merge 、ViewStub
- 线程池的原理和实现
- NDK设置C++编译器版本
- csharp: using using System.Web.Script.Serialization read json
- Java 多线程编程两个简单的例子
- yaf与silk环境搭建
- Android布局优化——抽象布局:include、merge 、ViewStub
- Java多线程同步工具之CyclicBarrier
- TabHost添加3个及以上tab时报的异常(you must specify a way to create the tab content)及其他
- 回溯之子集树法
- Using the Triangle Inequality to Accelerate k-Means
- OpenGL中的坐标变换、矩阵变换
- Java static(暂无static代码块内容)
- unix系统查看进程占用内存的方法
- PHP教程:PHP NOTICE级错误提示对程序性能影响的研究