Is it possible to get around either of these rules to use the merge
tag for a fragment's layout?
No. As you already seen, when you inflate a layout file which has the merge
tag as its root you must attach it to a valid parent ViewGroup
. Attaching it to the container in the onCreateView
is incorrect as the View
returned by that method will be added by the framework.
You could always just create a wrapper layout in the onCreateView
method to which to attach the inflated layout(and return this wrapper layout), but this will make the merge
tag optimization useless as you could add the wrapper layout in the xml layout file from the start:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout wrapper = new LinearLayout(getActivity()); // for example
inflater.inflate(R.layout.layout_with_merge_as_root, wrapper, true);
return wrapper;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…