Android Layout Tricks #4: Optimize, Part 2
2016-06-12 15:30
806 查看
Sharing and reusing layouts is very easy with Android thanks to the <include
/> tag, sometimes even too easy and you might end up with user interfaces that contain a large number of views, some of which are rarely used. Thankfully, Android offers a very special widget called ViewStub,
which brings you all the benefits of the
rarely used views.
A
no dimension, it does not draw anything and does not participate in the layout in any way. This means a
very cheap to inflate and very cheap to keep in a view hierarchy. A
as a lazy include. The layout referenced by a
inflated and added to the user interface only when you decide so.
The following screenshot comes from the Shelves application. The
main purpose of the activity shown in the screenshot is to present the user with a browsable list of books:
The same activity is also used when the user adds or imports new books. During such an operation, Shelves shows extra bits of
user interface. The screenshot below shows the progress bar and cancel button that appear at the bottom of the screen during an import:
Because
importing books is not a common operation, at least when compared to browsing the list of books, the import panel is originally represented by a
When
the user initiates the import process, the
inflated and replaced by the content of the layout file it references:
To
use a
you need is to specify an
to later inflate the stub, and an
to reference what layout file to include and inflate. A stub lets you use a third attribute,
which can be used to override the id of the root of the included file. Finally,
the layout parameters specified on the stub will be applied to the roof of the included layout.
Here is an example:
<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" />
When you are ready to inflate the stub, simply invoke the inflate() method.
You can also simply change the visibility of the stub to VISIBLE or INVISIBLE and
the stub will inflate. Note however that the
has the benefit of returning the root
the inflate layout:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
It is very important to remember that after the stub is inflated, the stub is removed from the view hierarchy. As such, it is unnecessary
to keep a long-lived reference, for instance in an class instance field, to a
A
views manually and adding them at runtime to your view hierarchy, simply use a
cheap and easy. The only drawback of
the <merge />
tag.
Happy coding!
原文URL:http://www.curious-creature.com/2009/03/16/android-layout-tricks-4-optimize-part-2/
/> tag, sometimes even too easy and you might end up with user interfaces that contain a large number of views, some of which are rarely used. Thankfully, Android offers a very special widget called ViewStub,
which brings you all the benefits of the
<include />without polluting your user interface with
rarely used views.
A
ViewStubis a dumb and lightweight view. It has
no dimension, it does not draw anything and does not participate in the layout in any way. This means a
ViewStubis
very cheap to inflate and very cheap to keep in a view hierarchy. A
ViewStubcan be best described
as a lazy include. The layout referenced by a
ViewStubis
inflated and added to the user interface only when you decide so.
The following screenshot comes from the Shelves application. The
main purpose of the activity shown in the screenshot is to present the user with a browsable list of books:
The same activity is also used when the user adds or imports new books. During such an operation, Shelves shows extra bits of
user interface. The screenshot below shows the progress bar and cancel button that appear at the bottom of the screen during an import:
Because
importing books is not a common operation, at least when compared to browsing the list of books, the import panel is originally represented by a
ViewStub:
When
the user initiates the import process, the
ViewStubis
inflated and replaced by the content of the layout file it references:
To
use a
ViewStuball
you need is to specify an
android:idattribute,
to later inflate the stub, and an
android:layoutattribute,
to reference what layout file to include and inflate. A stub lets you use a third attribute,
android:inflatedId,
which can be used to override the id of the root of the included file. Finally,
the layout parameters specified on the stub will be applied to the roof of the included layout.
Here is an example:
<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" />
When you are ready to inflate the stub, simply invoke the inflate() method.
You can also simply change the visibility of the stub to VISIBLE or INVISIBLE and
the stub will inflate. Note however that the
inflate()method
has the benefit of returning the root
Viewof
the inflate layout:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
It is very important to remember that after the stub is inflated, the stub is removed from the view hierarchy. As such, it is unnecessary
to keep a long-lived reference, for instance in an class instance field, to a
ViewStub.
A
ViewStubis a great compromise between ease of programming and efficiency. Instead of inflating
views manually and adding them at runtime to your view hierarchy, simply use a
ViewStub. It’s
cheap and easy. The only drawback of
ViewStubis that it currently does not support
the <merge />
tag.
Happy coding!
原文URL:http://www.curious-creature.com/2009/03/16/android-layout-tricks-4-optimize-part-2/
相关文章推荐
- Android布局技巧之使用ViewStub
- Android笔记之:深入ViewStub的应用
- Android组件ViewStub基本使用方法详解
- 深入分析Android ViewStub的应用详解
- Android布局优化之ViewStub控件
- 布局中的一些小知识
- Android性能优化之布局优化
- ViewStub的初步了解与merge和include的使用
- Android控件详解之惰性装载控件
- Lazy inflate之ViewStub
- android开发之布局优化之include、ViewStub、merge使用与源码分析
- Android Layout 优化
- ViewStub 源码实现
- ViewStub用于ListView和GirdView
- 动态加载布局ViewStub
- ViewStub must have a valid layoutResource
- ViewStub使用详解——从源码看ViewStub的使用
- Android小记:ViewStub的应用
- Android小记:ViewStub的使用
- Android布局优化之Merge Include ViewStub使用与源码分析