根据文档,如果我为XML资源文件中的<include>
标记设置了一个id,那么它应该覆盖包含的布局根视图的id。但是,它似乎并不奏效。用<include>覆盖android: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>
merge_layout.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");
}
然后它每次都抛出第二个异常。我错过了什么吗?
这是为什么不支持?难道是不是太复杂或甚至不可能在LayoutInflater中实现这一点?也许有人可以在这里发布补丁:https://source.android.com/source/submit-patches.html。我不太理解LayoutInflater膨胀视图的方式递归地也没有时间学习它,但如果有人能这样做会很棒...... –