2014-01-06 74 views
0

我想我的活動開始時,隱藏一個片段,我的佈局如下:無法隱藏與段管理片段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:background="#eee" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<fragment android:name="com.TypeFragment" 
      android:id="@+id/type_fragment" 
      android:layout_width="184dp" 
      android:layout_alignParentLeft="true" 
      android:layout_height="wrap_content" 
      android:text="@string/type" 
      /> 

<fragment android:name="com.ModeFragment" 
      android:id="@+id/mode_fragment" 
      android:layout_below="@id/type_fragment" 
      android:layout_width="184dp" 
      android:layout_alignParentLeft="true" 
      android:layout_height="wrap_content" 
      android:text="@string/mode" 
      /> 
<fragment 
    android:id="@+id/canvas_fragment" 
    android:name="com.CanvasFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/type_fragment" /> 

<fragment android:name="com.NumericsInputFragment" 
    android:id="@+id/numeric_area" 
    android:layout_below="@id/mode_fragment" 
    android:layout_width="184dp" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

我的MainActivity擴展FragmentActivity並具有以下的onCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_canvas); 

    Log.d("ACTIVITY" , "linside"); 
    // Check which layout we are using 
    if(findViewById(R.id.fragment_container) != null) { 
     /* if it is restored from a previous state - simply return */ 
     if (savedInstanceState != null) { 
       return; 
      } 
     /* Create a TypeFragment */ 
     TypeFragment firstFragment = new TypeFragment(); 
     //firstFragment.getView().setBackgroundColor(Color.GRAY); 
     ModeFragment secondFragment = new ModeFragment(); 
     /* This is the numerics Fragement */ 
     NumericsInputFragment numericsFragment = new NumericsInputFragment(); 

     /* Pass the Intents Extras to the Fragment */ 
     firstFragment.setArguments(getIntent().getExtras()); 
     secondFragment.setArguments(getIntent().getExtras()); 

     /* Add the fragment to the fragment container */ 
     getSupportFragmentManager().beginTransaction() 
      .add(R.id.fragment_container, firstFragment, null) 
      .add(R.id.fragment_container, secondFragment, null) 
      .add(R.id.fragment_container, numericsFragment, null) 
      .commit(); 

     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
      transaction.hide(getFragmentManager().findFragmentById(R.id.numeric_area)); 
      transaction.commit(); 
    } 
} 

我所做的一切似乎都對我的初始佈局有所影響。即如果我刪除所有的.add()語句,我仍然得到相同的佈局。很明顯,我在這裏做的事情非常錯誤,有人可以澄清我可能做錯了什麼嗎?

EDIT

我已替換爲更新的版本的佈局,像這樣一個的FrameLayout替換動態片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:background="#eee" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment android:name="com.TypeFragment" 
      android:id="@+id/type_fragment" 
      android:layout_width="184dp" 
      android:layout_alignParentLeft="true" 
      android:layout_height="wrap_content" 
      android:text="@string/type" 
      /> 

<fragment android:name="com.ModeFragment" 
      android:id="@+id/mode_fragment" 
      android:layout_below="@id/type_fragment" 
      android:layout_width="184dp" 
      android:layout_alignParentLeft="true" 
      android:layout_height="wrap_content" 
      android:text="@string/mode" 
      /> 
<fragment 
    android:id="@+id/canvas_fragment" 
    android:name="com.CanvasFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/type_fragment" /> 

<FrameLayout 
    android:id="@+id/numeric_area" 
    android:layout_width="184dp" 
    android:layout_height="match_parent" 
    android:layout_below="@id/mode_fragment" 
    android:background="?android:attr/detailsElementBackground" 
/> 
</RelativeLayout> 

然後我可以控制,如果在此描述http://developer.android.com/guide/components/fragments.html

這個顯示

回答

2

您無法添加/替換/隱藏在佈局文件中靜態定義的片段。您可以使用Framelayout並使用FragmentManager/FragmentTransaction將其替換爲片段實例。 Check out this link

+0

好的,謝謝...如果我使用的是相對佈局,是否有以相對佈局的正確方式以編程方式添加Fragment? – avrono

+0

RelativeLayout仍然可以是父級。您可以將FrameLayout(s)作爲其子(ren)。 這不是確切的代碼。這只是您可以構建的一個示例。 <的FrameLayout 機器人:ID = 「@ + ID /的FrameLayout> 機器人:layout_width =」 FILL_PARENT」 機器人:layout_height = 「FILL_PARENT」>