我制作复杂的布局,并使用"include"作为我的自定义组件,就像这样
<include layout="@layout/topbar"/>
Run Code Online (Sandbox Code Playgroud)
并且topbar定义如下:
<?xml version="1.0" encoding="utf-8"?>
<my.package.TopBarLayout
... a lot of code
Run Code Online (Sandbox Code Playgroud)
现在,我想将我的自定义属性传递给"topbar",就像这样
<include layout="@layout/topbar" txt:trName="@string/contacts"/>
Run Code Online (Sandbox Code Playgroud)
但我没有结果.我从那个页面了解到我不能设置任何属性,而是id,height和width.
那么,我如何将我的自定义属性传递给包含,以及如何使其工作?
我有一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我的xml布局有一些布局(例如,线性)作为根元素,它可以正常工作.但是当我尝试<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>
Run Code Online (Sandbox Code Playgroud)
我还尝试从<merge... >tag中删除所有属性,得到相同的结果.怎么了?
更新:上面的代码是正确的.正如秘密所提到的那样,问题是在另一件od代码中<merge>被用作root element和膨胀:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
R.layout.layers_list_item,
R.id.layers_list_item_text);
Run Code Online (Sandbox Code Playgroud)
随着时间一天天加入的元素适配器试图夸大R.layout.layers_list_item其具有<merge>作为根.