Android 如何<;包括>;带有片段标记的布局文件?
我未能创建android布局文件activity_item_list.xml,其内容如下所示:Android 如何<;包括>;带有片段标记的布局文件?,android,include,fragment,Android,Include,Fragment,我未能创建android布局文件activity_item_list.xml,其内容如下所示: <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/item_list" android:name="xxx.xxx.xxx.ItemListFragment"
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="xxx.xxx.xxx.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
好吧,它会崩溃的。事故日志如下:
android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
android.view.InflateException:二进制XML文件行#1:膨胀类时出错
我将在此处列出所有步骤:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ItemListActivity" >
<fragment
android:id="@+id/item_list"
android:name="xxx.xxx.xxx.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout
android:id="@+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
修改后的文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ItemListActivity" >
<include
layout="@layout/activity_item_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<include
layout="@layout/activity_item_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
现在,第二个include块运行良好。但是第一个会导致崩溃
活动\u项目\u detail.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" />
我试图修改activity_item_list.xml,如下所示,但仍然崩溃
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/item_list"
android:name="xxx.xxx.xxx.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
</FrameLayout>
我不知道为什么android不能支持一个片段。谁能告诉我。谢谢大家! 您能提供更多的上下文吗,比如您是否试图自行膨胀碎片?如果是这样的话,据我所知它不会起作用。它必须嵌入到另一个布局中。 您可以使用此选项了解有关使用片段的更多信息:
希望它能帮助你,你是如何在你的版面中使用它的?请看我的新帖子。谢谢您是否也可以为activity_item_detail粘贴xml?在片段中定义的类路径是否正确?附加activity_item_detail.xml。您已经编写了第一个文件的名称为“activity_item_detail.xml”。这里有一个拼写错误:应该是'detail'而不是'detial'。如果在实际代码中是相同的,它将不会运行。请让我知道如果他修好了,这是我帖子中的一个错误。很抱歉不在代码中。。。实际上,文件名是由向导生成的,我从未更改过。