根据文档,如果我<include>
为 XML 资源文件中的标签设置了一个 id,那么它应该覆盖包含布局的根视图的 id。但是,它似乎不起作用。
我创建了一个非常简单的项目来演示它:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/merge_layout" />
</RelativeLayout>
合并布局.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</merge>
现在如果我运行这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.button) == null)
throw new RuntimeException("button is null"); // Never happens
if (findViewById(R.id.test) == null)
throw new RuntimeException("test is null");
}
然后它每次都会抛出第二个异常。我错过了什么吗?