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

我的活动将其所有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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...