2013-11-15 48 views
11

我的活動將其所有GUI片段聲明爲單個XML佈局。它只需要在啓動時顯示一些片段;當用戶與應用程序交互時,剩下的部分就會顯示出來。佈局的一部分如下:如何聲明片段隱藏在XML佈局中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <fragment 
     android:id="@+id/map_panel" 
     android:name="com.example.MapPanel" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/map_panel_height" /> 
    <fragment 
     android:id="@+id/list_panel" 
     android:name="com.example.ListPanel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/map_panel" /> 
    <fragment 
     android:id="@+id/detail_panel" 
     android:name="com.example.DetailPanel" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/map_panel" 
     android:visibility="gone" /> 

我的意圖是,list_panel片段是可見的在啓動時,以及直到用戶從列表中選擇東西detail_panel片段是隱藏的。

默認情況下,片段始於isHidden屬性爲false。這意味着我的活動必須迭代所加載的片段,並在啓動時對detail_panel等片段手動調用isHidden(true)

我寧願在XML佈局中聲明isHidden狀態。但是,在<fragment>聲明中設置android:visibility="gone"並不會更改isHidden狀態,並且我無法找到任何有用的技術文檔。

是否可以在<fragment>上設置XML屬性以使其隱藏?

注意:我不關心視圖的可見性,我關注fragment.isHidden()的值。這會影響FragmentManager如何操縱後退堆棧並執行動畫。如果在片段的視圖不可見或消失的情況下調用transaction.show(fragment),但fragment.isHidden()值爲false,則FragmentManager將不會使視圖可見。請參閱http://developer.android.com/reference/android/app/Fragment.html#isHidden()以供參考。

+0

你試過android:visibility =「invisible」嗎? – Sushil

+0

是的,我除了「走了」之外,還嘗試過「隱形」。既不影響片段的isHidden狀態。 –

+0

你是什麼意思的「除了去」。你只是嘗試「隱形」。在這種情況下,它會創建碎片並將其保持在隱形狀態。 – Sushil

回答

-1

我們有isVisible方法的片段 可見可見性Gone不佔用任何空間 Invisble的地方佔用實際的視圖空間。

+0

您正在描述視圖的狀態,而不是片段的「isHidden」狀態。 –

2

這個答案有點晚了,可能對未來的參考很有幫助。可見性是View類的一部分 - Fragment擴展對象,但無法訪問可見性值。一種可能性是使片段成爲FrameLayout的子元素,並且調用不可見或者在佈局上不起作用。這將導致片段顯示爲隱藏。

希望它有幫助!

+0

更改View的'visibility'狀態對片段的'isHidden'狀態沒有影響。 –

+1

圍繞「FrameLayout」包裝「fragment」是最簡單和最有效的IMHO – noloman

0
public void showHideFrgament(final Fragment fragment){ 

    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
        ft.setCustomAnimations(android.R.animator.fade_in, 
          android.R.animator.fade_out); 

    if (fragment.isHidden()) { 
        ft.show(fragment); 
        Log.d("hidden","Show"); 
       } else { 
        ft.hide(fragment); 
        Log.d("Shown","Hide");       
       } 
       ft.commit(); 

} 
4

我面臨着類似的情況,我必須隱藏一個片段。

我只是將片段包含在LinearLayout中,並將佈局標記爲可見/消失。

<LinearLayout 
    android:id="@+id/map_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="visible" > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" /> 
</LinearLayout> 
3

基於關閉Jyo的帖子,用這個:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
    fragmentTransaction.hide(mFragment); 
    fragmentTransaction.commit(); 

這爲我工作在API級別23「mFragment」就是你要隱藏的片段。

相關問題