ViewStub和include布局使用(官方文档)

来源:互联网 发布:博士德汽修软件 编辑:程序博客网 时间:2024/06/05 21:59

Loading Views On Demand

Sometimes your layout might require complex views that are rarely used. Whether they are item details, progress indicators, or undo messages, you can reduce memory usage and speed up rendering by loading the views only when they are needed.

Define a ViewStub


ViewStub is a lightweight view with no dimension and doesn’t draw anything or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. Each ViewStub simply needs to include the android:layout attribute to specify the layout to inflate.

The following ViewStub is for a translucent progress bar overlay. It should be visible only when new items are being imported into the application.

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

Load the ViewStub Layout


When you want to load the layout specified by the ViewStub, either set it visible by calling setVisibility(View.VISIBLE) or call inflate().

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// orView importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

Note: The inflate() method returns the inflated View once complete. so you don't need to call findViewById() if you need to interact with the layout.

Once visible/inflated, the ViewStub element is no longer part of the view hierarchy. It is replaced by the inflated layout and the ID for the root view of that layout is the one specified by the android:inflatedId attribute of the ViewStub. (The ID android:id specified for the ViewStub is valid only until theViewStub layout is visible/inflated.)

Note: One drawback of ViewStub is that it doesn’t currently support the <merge> tag in the layouts to be inflated.

Re-using Layouts with <include/>

Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. It also means that any elements of your application that are common across multiple layouts can be extracted, managed separately, then included in each layout. So while you can create individual UI components by writing a custom View, you can do it even more easily by re-using a layout file.Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout.

Create a Re-usable Layout


If you already know the layout that you want to re-use, create a new XML file and define the layout. For example, here's a layout from the G-Kenya codelab that defines a title bar to be included in each activity (titlebar.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/titlebar_bg">    <ImageView android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:src="@drawable/gafricalogo" /></FrameLayout>

The root View should be exactly how you'd like it to appear in each layout to which you add this layout.

Use the <include> Tag


Inside the layout to which you want to add the re-usable component, add the <include/> tag. For example, here's a layout from the G-Kenya codelab that includes the title bar from above:

Here's the layout file:

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

You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the<include/> tag. For example:

<include android:id="@+id/news_title"         android:layout_width="match_parent"         android:layout_height="match_parent"         layout="@layout/title"/>

However, if you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_widthin order for other layout attributes to take effect.

Use the <merge> Tag


The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.

To avoid including such a redundant view group, you can instead use the <merge> element as the root view for the re-usable layout. For example:

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

Now, when you include this layout in another layout (using the <include/> tag), the system ignores the <merge> element and places the two buttons directly in the layout, in place of the <include/> tag.


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 京东退款不小心点了取消退款怎么办 近邻宝开了箱又关了怎么办 近邻宝箱子打开了东西忘记拿怎么办 京东取消订单商家总不取消怎么办 京东快递退货取件一直取不到怎么办 在京东线上付款了但没收到货怎么办 寄快递收件人电话号码写错了怎么办 京东购物实名认证被别人占用怎么办 货已发出单号还没填买家退货怎么办 自提柜还有一个包忘记拿怎么办 当顾客说衣服太贵的时候怎么办 京东快递送错了被别人签收了怎么办 乐视1s玩王者荣耀卡怎么办 京东商城买东西发现地址错了怎么办 京东分期付款买手机额度不够怎么办 亮皮银色高跟鞋时间放久变色怎么办 饿了么商家钱包提现被锁了怎么办 在淘手游买的游戏账号被找回怎么办 交易猫买手游梦幻号被找回了怎么办 绑定着苹果账号的邮箱忘记了怎么办 京东注册账号跟密码都忘记了怎么办 孩子大学一个宿舍宿友不行怎么办 微信手机支付密码忘记了怎么办 微信公众号不能付款了怎么办 头条误点了投放头条广告怎么办 交行网银密码错误6次怎么办 宁波新冮厦关门我们的消费卡怎么办 第一天上班别人教的学不会怎么办 老公出轨已经不想跟你爱爱了怎么办 妻子出轨丈夫为了孩子不离婚怎么办 妻子出轨分居死也不愿意离婚怎么办 拼多多个人卖家登录密码忘了怎么办 微信被限制添加更多订阅号了怎么办 知网复制粘贴的论文有格式怎么办 知网论文查询网站密码忘记了怎么办 开通了超级会员不能克隆好友怎么办 偷了家里好多钱去买手机了怎么办 中行信用卡短信邀请办白金卡怎么办 注册邮箱说邮箱地址已被注册怎么办 便利店开在小区里面没生意怎么办 微信支付密码忘了怎么办没银行卡