我的活動將其所有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()以供參考。
你試過android:visibility =「invisible」嗎? – Sushil
是的,我除了「走了」之外,還嘗試過「隱形」。既不影響片段的isHidden狀態。 –
你是什麼意思的「除了去」。你只是嘗試「隱形」。在這種情況下,它會創建碎片並將其保持在隱形狀態。 – Sushil