片段显示在xml预览中,但不会在应用启动Android时显示

时间:2014-11-06 01:30:55

标签: fragment

我有一个使用2个片段的应用程序并排显示。当我添加另一个片段视图时,它不会显示。这是我的代码: 该片段名为@ + id / fragment1,位于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:orientation="horizontal" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/searchTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/searchBtn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.37" >

        <fragment
            android:id="@+id/fragment"
            android:name="com.davila.fundamentals.MasterFragment"
            android:layout_width="145dp"
            android:layout_height="match_parent" />

        <View
            android:id="@+id/dummy_view"
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />

        <RelativeLayout
            android:id="@+id/fragment2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" >

            <fragment
                android:id="@+id/fragment1"
                android:name="com.davila.fundamentals.PreferenceFragment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我弄明白为什么片段没有显示。事实证明,我没有在片段实例的末尾调用commit()方法:

getFragmentManager()的BeginTransaction()代替(...)提交();

这解决了这个问题。