Layout Resource官方教程(4)<include>与<merge>
Re-using Layouts with <include/>
THIS LESSON TEACHES YOU TO
YOU SHOULD ALSO READ
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.
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.
可以在一个layout中使用 <include/>
and <merge/>
嵌入其它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
):
下面是一个可复用的layout,它将要被其它layout include
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width=”match_parent” 3 android:layout_height="wrap_content" 4 android:background="@color/titlebar_bg"> 5 6 <ImageView android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:src="@drawable/gafricalogo" /> 9 </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:
<include>示例
Here's the layout file:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width=”match_parent” 4 android:layout_height=”match_parent” 5 android:background="@color/app_bg" 6 android:gravity="center_horizontal"> 7 8 <include layout="@layout/titlebar"/> 9 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>
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.
可以在 <include>中使用android:layout_* 系列属性会重写被include的layout的根标签的同名属性,一但果重写其中一个,
就必需要重写
android:layout_height
andandroid:layout_width
For example:
1 <include android:id=”@+id/news_title” 2 android:layout_width=”match_parent” 3 android:layout_height=”match_parent” 4 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_width
in 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.
当一个layout include 另一个layout时,<merge>用来去除重复的布局标签.
举例:两个layout A和B,其中A是垂直LinearLayout且include B,如果B也是垂直LinearLayout,那么就会在A的LinearLayout中嵌入另一个同样的Layout,
没有意义,反而会减慢速度。这时可以把B的根标签写成 <merge xmlns:android="http://schemas.android.com/apk/res/android" >
注意 <merge>只能作根标签,且系统会忽视它。
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:
1 <merge xmlns:android="http://schemas.android.com/apk/res/android"> 2 3 <Button 4 android:layout_width="fill_parent" 5 android:layout_height="wrap_content" 6 android:text="@string/add"/> 7 8 <Button 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/delete"/> 12 13 </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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?