2016-05-20 143 views
0

嗨我想顯示我創建的片段。我通過互聯網搜索任何解決方案,但沒有找到任何解決方案。Android片段不會顯示

這是我的代碼:

線條調用片段:

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(); 
    } 
} 

任何想法將大大appreciated.Thanks。

回答

0

我發現了問題 - 的FrameLayout應該是 活動的佈局其中的片段屬於

+0

是的,你是對的,但你怎麼忘了:) –

+0

我真的不知道:) –

0

嘗試調用另一個包含片段容器ID的方法。舉一個例子fragmentTransaction.add(R.id.fragmentContainer,editStudent, "editStudent").commit();

+0

試了一下里面。不工作:( –

+0

分享您的活動佈局 – Alexander

0

你需要給你的框架佈局,我會傳遞片段。

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

嘗試過。不工作:( –