【发布时间】:2016-09-17 21:23:08
【问题描述】:
您好,我正在尝试展示我创建的片段。我在互联网上搜索任何解决方案,但没有找到任何解决方案。
这是我的代码:
行调用片段:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
EditStudent editStudent = new EditStudent();
fragmentTransaction.add(editStudent, "editStudent").commit();
EditStudent 扩展 Fragment。这是我在onCreateView 方法中返回的内容:
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>
当我单击执行所有事务代码的按钮时,我仍然看到的是活动布局而不是片段布局。我没有任何例外,只是片段不会显示。我也知道片段已成功创建,因为当我按下后退按钮时,我会使用以下代码从堆栈中弹出片段:
@Override
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() == 0)
{
super.onBackPressed();
}
else
{
getFragmentManager().popBackStack();
}
}
任何想法将不胜感激。谢谢。
【问题讨论】:
标签: android layout transactions fragment screen