有关布局别名不正确的Andr​​oid文档? [英] Incorrect Android documentation about layout aliases?

查看:157
本文介绍了有关布局别名不正确的Andr​​oid文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何重用或别名布局用最少的样板code。

看来,<一个href="http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters">Android文档中的有关版面别名是不正确的,肯定会出现不一致。这部分文档显示以下布局文件为例:

 &LT;资源&GT;
    &LT;项目名称=主类型=布局&GT; @布局/ main_twopanes&LT; /项目&GT;
&LT; /资源&GT;
 

如果我尝试编译,我得到一个属性丢失了Android命名空间preFIX 错误。即使加上命名空间的资源元素中,我得到错误:错误:不允许字符串类型(在类型,值为布局)

<一个href="http://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources">Elsewhere在Android文档,他们表现出不同的,看似倒和不正确的方式来别名布局:

  

要创建一个别名,以现有的布局,使用元素,   包裹在一个&LT;合并&GT; 。例如:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;合并&GT;
    &LT;包括布局=@布局/ main_ltr/&GT;
&LT; /合并&GT;
 

运行这导致在LogCat中 E / AndroidRuntime(1558)以下错误:android.view.InflateException:其中,合并/&GT;只能用一个有效的ViewGroup根attachToRoot = true时使用。所以,这个错误似乎增强的事实,这&LT;包括&GT; &LT;合并&GT; 对必须是一个错误,因为它需要一个不必要的父母查看

最后还有的<一个href="http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge"><$c$c><merge>文档,这似乎违背了以前的方向,使得没有提及的一个顶级的反转形式&LT;合并&GT;&LT;有/&GT;&LT; /合并&GT;

  

要避免包括这样的冗余视图组,您可以改用   该元件作为可重复使用的布局根视图。对于   例如:

 &LT;合并的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;

    &LT;按钮
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/添加/&GT;

    &LT;按钮
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/删除/&GT;

&LT; /合并&GT;
 

解决方案

第一种技术的工作原理,你只要把你的&LT;资源&GT; 在正确的文件夹中的文件。它应该在你可能通过重用布局时,文件夹不是布局文件夹&LT;包括&GT;

例如,假设你有一个名为 editor.xml 的布局,生活中的布局文件夹。假设你想使用正常的屏幕尺寸专门设计。如果你不关心重复自己,你只需复制此布局粘贴到布局小布局正常文件夹并将其命名为 editor.xml 的每个文件夹中。所以,你有一个名为三个文件 editor.xml

如果你不想重复自己,你将专门布置在主布局文件夹并将其命名,比如, compact_editor的.xml 。然后,你会创建一个名为 layout.xml 值小值文件-Normal 文件夹。每个文件内容如下:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
    &LT;项目名称=编辑器类型=布局&GT; @布局/ compact_editor&LT; /项目&GT;
&LT; /资源&GT;
 

我已经提交了文档问题有关的另外两个问题

I'd like to figure out how to reuse or "alias" layouts with the least boilerplate code.

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>

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').

Elsewhere in the Android documentation, they show a different and seemingly inverted and incorrect way to alias layouts:

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>

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.

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>

解决方案

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

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.

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.

这篇关于有关布局别名不正确的Andr​​oid文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆