关于布局别名的Android文档不正确? [英] Incorrect Android documentation about layout aliases?
问题描述
我想知道如何重用或别名布局与最少的样板代码。
I'd like to figure out how to reuse or "alias" layouts with the least boilerplate code.
似乎关于布局别名的Android文档是不正确的,当然会出现不一致。本部分文档显示以下布局文件为例:
It seems that the Android documentation about layout aliases is incorrect, and certainly appears inconsistent. This section of documentation shows the following layout file as an example:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
如果我尝试编译,我得到一个属性缺少Android命名空间前缀
错误。即使将命名空间添加到资源
元素后,我得到错误:错误:不允许使用字符串类型('type',值为'layout') 。
If I try to compile this, I get an Attribute is missing the Android namespace prefix
error. Even after adding the namespace to the resources
element, I get error: Error: String types not allowed (at 'type' with value 'layout').
在Android文档中的其他地方,它们显示了一种不同的,看似颠倒和不正确的别名布局方式:
Elsewhere in the Android documentation, they show a different and seemingly inverted and incorrect way to alias layouts:
要创建现有布局的别名,请使用包含在
< merge>
中的元素
。例如:
To create an alias to an existing layout, use the element, wrapped in a
<merge>
. For example:
<?xml version="1.0" encoding="utf-8"?>
<merge>
<include layout="@layout/main_ltr"/>
</merge>
运行此结果会导致LogCat中的以下错误 E / AndroidRuntime(1558):android.view.InflateException:< merge />只能使用一个有效的ViewGroup根和attachToRoot = true
。所以这个错误似乎加强了这个事实:这个< include>
< merge>
pair必须是一个错误,因为它需要一个不必要的父母查看
。
Running this results in the following error in LogCat E/AndroidRuntime(1558): android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
. So this error seems to reinforce the fact that this <include>
<merge>
pair must be a mistake, because it requires an unnecessary parent View
.
最后还有一个 < merge>
文档,与前一个方向相矛盾,没有提到顶级< merge>< include />< / merge>
的倒置形式。
Lastly there's the <merge>
documentation, which seems to contradict the former direction, making no mention of the inverted form of a top-level <merge><include/></merge>
.
为避免包含这样一个冗余视图组,您可以使用
该元素作为可重用布局的根视图。对于
示例:
To avoid including such a redundant view group, you can instead use the 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>
推荐答案
工作,您只需将< resources>
文件放在正确的文件夹中。它应该在值
文件夹中,而不是布局
文件夹,您可以通过重用布局< include>
。
The first technique works, you just have to put your <resources>
file in the correct folder. It should be in the values
folders not the layout
folders as you might when reusing layouts via <include>
.
例如,假设您有一个名为 editor.xml
,它们位于布局
文件夹中。假设您想在 small
和正常
屏幕尺寸上使用专门的布局。如果您不在乎重复自己,您只需将此布局复制并粘贴到布局小
和布局正常
文件夹,并在每个文件夹中命名为$ code> editor.xml 。所以你会有三个名为 editor.xml
的文件。
For instance, suppose you have a layout named editor.xml
that lives in the layout
folder. Suppose you want to use a specialized layout on small
and normal
screen sizes. If you didn't care about repeating yourself, you would just copy and paste this layout into the layout-small
and layout-normal
folders and name it editor.xml
in each folder. So you'd have three files named editor.xml
.
如果你不想重复,您将将专门的布局放在主要的布局
文件夹中,并将其命名为 compact_editor.xml
。然后,您将在 values-small
和值中创建一个名为
文件夹。每个文件将显示为: layout.xml
的文件-normal
If you don't want to repeat yourself, you would place the specialized layout in the main layout
folder and name it, say, compact_editor.xml
. Then you'd create a file named layout.xml
in the values-small
and values-normal
folders. Each file would read:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="editor" type="layout">@layout/compact_editor</item>
</resources>
我已提交文档问题其他两个问题。
I've filed a documentation issue about the other two problems.
这篇关于关于布局别名的Android文档不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!