android – 如何将片段声明为隐藏在XML布局中

前端之家收集整理的这篇文章主要介绍了android – 如何将片段声明为隐藏在XML布局中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的活动将其所有GUI片段声明为单个 XML布局.它只需要在启动时显示几个片段;当用户与应用程序交互时,其余的显示.布局的一部分如下:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" >
  4. <fragment
  5. android:id="@+id/map_panel"
  6. android:name="com.example.MapPanel"
  7. android:layout_width="match_parent"
  8. android:layout_height="@dimen/map_panel_height" />
  9. <fragment
  10. android:id="@+id/list_panel"
  11. android:name="com.example.ListPanel"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_below="@+id/map_panel" />
  15. <fragment
  16. android:id="@+id/detail_panel"
  17. android:name="com.example.DetailPanel"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"
  20. android:layout_below="@+id/map_panel"
  21. 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中,并将布局标记为可见/不可见.

  1. <LinearLayout
  2. android:id="@+id/map_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:visibility="visible" >
  6.  
  7. <fragment
  8. android:id="@+id/map"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. class="com.google.android.gms.maps.MapFragment" />
  12. </LinearLayout>

猜你在找的Android相关文章