Android XML 布局中 <merge> 和 <include> 用法的简单示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2732682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple example of <merge> and <include> usage in Android XML-layouts
提问by aioobe
I'm curious about the <merge>
and <include>
tags in Android XML-layouts. I've read two tutorials, but haven't yet found a simple example usage.
我对Android XML 布局中的<merge>
和<include>
标签很好奇。我已经阅读了两个教程,但还没有找到一个简单的示例用法。
Would be happy if someone could provide such an example or give a pointer to one.
如果有人可以提供这样的例子或给一个指针,会很高兴。
回答by yanchenko
some_activity.xml:
some_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
// some views
<include layout="@layout/view_part"/>
// probably more views
</LinearLayout>
view_part.xml:
view_part.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
// the views to be merged
</merge>
回答by Habeeb Perwad
Take an example:
举个例子:
I have two tags <EditText>
and <ListView >
coming more than one UIs.
So I created an XML file as given below to include in all such UI's.
我有两个标签,<EditText>
并且<ListView >
有不止一个 UI。因此,我创建了一个如下所示的 XML 文件以包含在所有此类 UI 中。
<?xml ...>
<EditText ... />
<ListView ... />
The above XML is not valid XML since it did not have a root element.
So a root element is needed just for the sake of XML. <merge>
is the solution as given below:
上述 XML 不是有效的 XML,因为它没有根元素。所以为了 XML 需要一个根元素。<merge>
是下面给出的解决方案:
<?xml ...>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<EditText ... />
<ListView ... />
</merge>
回答by bk138
There's a simple Android XML layout <include /> HOWTO that's also explaining a common pitfall over at http://www.coboltforge.com/2012/05/tech-stuff-layout/. That may help...
有一个简单的 Android XML 布局 <include /> HOWTO 也解释了http://www.coboltforge.com/2012/05/tech-stuff-layout/ 上的一个常见陷阱。这可能会有所帮助...
回答by dev
id doesn't paste code otherwise relative layout parameters would have worked. It does some different processing
id 不粘贴代码,否则相对布局参数会起作用。它做了一些不同的处理
回答by huseyin
<merge>
tag is used to mitigate the number of the levels to increase the performance of rendering layouts. tag is used with <include>
tag perfectly together.
<merge>
标签用于减少级别数量以提高渲染布局的性能。tag 与<include>
tag 完美结合使用。
Take an example, we have a login layout and used for more than one in scope of our app. While using tag to show login_layout, we can use and can escape a level.
举个例子,我们有一个登录布局,在我们的应用程序范围内用于多个登录布局。在使用标签显示 login_layout 时,我们可以使用并且可以转义一个级别。
I also advise you to read the tricks about layouts. http://android-developers.blogspot.com.tr/2009/03/android-layout-tricks-3-optimize-by.html
我还建议您阅读有关布局的技巧。 http://android-developers.blogspot.com.tr/2009/03/android-layout-tricks-3-optimize-by.html
login_form.xml
登录表单.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Login form -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email..."
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:visibility="visible" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password.."
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:text="1337"
android:visibility="visible" />
<Button
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16sp"
android:paddingLeft="32sp"
android:paddingRight="32sp"
android:text="Login"
android:visibility="visible" />
</LinearLayout>
example_layout.xml(any layout we want to include login_form.xml)
example_layout.xml(我们想要包含 login_form.xml 的任何布局)
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<include layout="@layout/login_form" />
</merge>
We can see the level hierarchy
我们可以看到级别的层次结构