Android抽象布局——include、merge 、ViewStub
来源:互联网 发布:淘宝手机店铺宝贝分类 编辑:程序博客网 时间:2024/05/17 00:06
在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。
1、布局重用<include />
<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: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
>
1)<include />标签可以使用单独的layout属性,这个也是必须使用的。
2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖,解决方案。
3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_width和layout_height两个属性。
4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决(参考):
View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);
bookmarks_container_2.findViewById(R.id.bookmarks_list);
2、减少视图层级<merge />
<merge />标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge />多用于替换FrameLayout或者当一个布局包含另一个时,<merge />标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge />标签优化。
<
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
>
现在,当你添加该布局文件时(使用标签),系统忽略节点并且直接添加两个Button。
3、需要时使用<ViewStub />
<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0的View。<ViewStub />标签使用如下:
<
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 />标签。
注意:如果你需要和这个Layout交互,inflate()
方法会在渲染完成后返回被inflate的视图,所以你不需要再调用findViewById()
去查找这个元素。
一旦ViewStub
可见或是被inflate了,ViewStub
元素就不存在了。取而代之的是被inflate的Layout,其id是ViewStub
上的android:inflatedId
属性。(ViewStub
的android:id
属性仅在ViewStub
可见以前有用)
注意:ViewStub
的一个缺陷是,它目前不支持使用<merge>
标签的Layout。
使用ViewStub时的技巧: 如果你在Layout中放置了多个ViewStub
,你可以使用下面的方法来简化Layout文件: 保留一个ViewStub
,在Activity
的onCreate()
方法中执行如下代码:
setContentView(R.layout.activity_main);ViewStub = stub = (ViewStub) findViewById(R.id.stub);stub.setInflateId(R.id.view1);stub.setLayoutResource(R.layout.my_layout.xml);View view1 = stub.inflate();
在运行时动态地设置ViewStub
的资源文件和inflateId
,然后调用inflate()
方法生成View
。
- 【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
- 逆向工程核心原理学习笔记2-基址重定位基本原理
- hdu 2871(区间合并)
- 网页要展示大量图片时怎样延迟加载图片 lyz.delayLoading.min.js
- Python 集合set
- ZFS Architecture Description
- Android抽象布局——include、merge 、ViewStub
- 51nod 1201 整数划分(锻炼思维的好题)
- Maven:基本命令 / Demo
- 谱聚类的增量更新原理
- hdoj 2087 剪花布条 【kmp(变形题)】
- ioS开发之UI基础--UIScrollView的基本使用
- Condition
- Codeforces 567E President and Roads 【最短路】
- c++primer之类(类的静态成员)