【发布时间】:2012-12-26 10:20:13
【问题描述】:
我有一个CustomView 类,我想为它使用xml 布局。因此,我的课程扩展了RelativeLayout,扩展了 xml 布局并尝试将其附加到自身。
public class CustomView extends RelativeLayout
{
public CustomView (Context context)
{
super(context);
LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true);
}
}
如果我的 xml 布局有一些布局(例如线性)作为根元素,它可以正常工作。但是当我尝试根据this response 使用<merge> 标签时,我得到了这个错误:
<merge /> can be used only with a valid ViewGroup root and attachToRoot=true
我的xml布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
... >
<CheckBox
... />
<TextView
... />
</merge>
我还尝试从<merge... > 标记中删除所有属性,得到了相同的结果。怎么了?
UPDATE:上面的代码是正确的。
正如secretlm 提到的,问题在于<merge> 被用作root element 并在另一段代码中膨胀:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
R.layout.layers_list_item,
R.id.layers_list_item_text);
随着添加的每个元素,适配器都试图膨胀R.layout.layers_list_item,它以<merge> 为根。
【问题讨论】:
-
你能在
标签中更新你的孩子的观点吗?