如何<包含片段标签的布局文件?</include>

时间:2013-03-26 08:25:49

标签: 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"
    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>

我将在此列出所有步骤:

  1. 在eclipse中,通过向导新建项目并选择主/详细流程

  2. 向导完成后,
  3. 我得到了4个由向导生成的布局xmls:activity_item_detail.xml,activity_item_twopane.xml,activity_item_list.xml和fragment_item_detail.xml

  4. 让我们修改activity_item_twopane.xml。我想重用一些布局。

  5. orignal activity_item_twopane.xml:

    <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>
    

    现在,第二个包含块运行良好。但是第一个会导致崩溃。

    activity_item_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无法支持片段。谁能告诉我谢谢!

1 个答案:

答案 0 :(得分:1)

请你提供更多上下文,比如你试图自己给片段充气吗?如果是这样,就我所知,它将无法运作。它必须嵌入另一个布局中。 您可以使用它来了解有关使用片段的更多信息: http://developer.android.com/guide/components/fragments.html

希望有所帮助

相关问题