Android 片段不会出现

Android fragment won't show up

您好,我正在尝试展示我创建的片段。我在互联网上搜索任何解决方案,但没有找到。

这是我的代码:

行调用片段:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
EditStudent editStudent = new EditStudent();
fragmentTransaction.add(editStudent, "editStudent").commit();

EditStudent 扩展 Fragment。这是我在 onCreateView 方法中 returns 的内容:

public class EditStudent extends Fragment
{
    @Override
    public View onCreateView (LayoutInflater inflater,
                          ViewGroup container,
                          Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.activity_edit_student, container, false);
    }
}

这是我的 activity_edit_student 布局:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="left"
android:layoutDirection="ltr"
tools:context="com.nitay.uziel.students.StudentsList"
android:orientation="vertical">

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

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

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

                <ImageView
                    android:id="@+id/studentPicture"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:layout_span="2"/>

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name: "
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/studentName"
                    android:layout_width="250dp"
                    android:maxLength="20"
                    android:layout_height="wrap_content"/>

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ID: "
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/studentId"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Phone: "
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/studentPhone"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:maxLength="10"/>

            </TableRow>

        </TableLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true">

        <ImageButton
            android:background="@drawable/cancel"
            android:id="@+id/cancelStudentButton"
            android:layout_height="70dp"
            android:layout_width="70dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="20dp"/>

        <ImageButton
            android:background="@drawable/confirm"
            android:id="@+id/confirmStudentButton"
            android:layout_height="70dp"
            android:layout_width="70dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="20dp"/>

    </RelativeLayout>
</FrameLayout>

当我点击执行所有事务代码的按钮时,我仍然看到 activity 布局而不是片段布局。我没有任何例外,只是片段不会显示。我也知道片段已成功创建,因为当我按下后退按钮时,我使用以下代码从堆栈中弹出片段:

@Override
public void onBackPressed()
{
    if (getFragmentManager().getBackStackEntryCount() == 0)
    {
        super.onBackPressed();
    }
    else
    {
        getFragmentManager().popBackStack();
    }
}

任何想法都会很多appreciated.Thanks。

尝试调用另一个包含片段容器 ID 的方法。例如 fragmentTransaction.add(R.id.fragmentContainer,editStudent, "editStudent").commit();

您需要提供框架布局并将片段传递给它。

fragmentManager.beginTransaction().add(R.id.fragment_container, editStudent, "editStudent" ).commit();

我发现了问题 - FrameLayout 应该在 activity 片段属于谁