我的活动将其所有GUI片段声明为单个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()参考.
最佳答案
我面临着类似的情况,在那里我不得不隐藏一个片段.
我简单地将片段包含在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>
相关文章
转载注明原文:android – 如何将片段声明为隐藏在XML布局中 - 代码日志