I have a CustomView
class and I want to use xml layout for it. So, my class extends RelativeLayout
, inflates xml layout and tries to attach it to self.
public class CustomView extends RelativeLayout
{
public CustomView (Context context)
{
super(context);
LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true);
}
}
If my xml layout has some layout (Linear, for example) as root element it works fine. But when I try to use <merge>
tag according to this response I got this error:
<merge /> can be used only with a valid ViewGroup root and attachToRoot=true
My xml layout is like:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
... >
<CheckBox
... />
<TextView
... />
</merge>
I also tried to remove all attributes from <merge... >
tag, got the same result. What's wrong?
UPDATE: The code above is correct.
As secretlm mentioned, problem was that <merge>
was used as a root element
and inflated in another piece od code:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
R.layout.layers_list_item,
R.id.layers_list_item_text);
And with every element added adapter tried to inflate R.layout.layers_list_item
which have <merge>
as root.