尽管 android:visibility="gone" 地图片段显示为可见

Map Fragment shows as visible despite android:visibility="gone"

我正在为一个用于较小屏幕的应用开发另一种简化布局。此布局应 包含在较大布局中使用的地图片段,但是,我应该尽可能避免程序更改。显示布局的 activity 也使用片段的 ID 处理片段,所以我不能只删除它。我试着让片段不可见,像这样:

<fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />

但片段仍然可见。当我将可见性设置为 "invisible" 时,也会发生同样的情况。片段的默认位置被视图部分隐藏,但是当我在布局中移动片段时,无论我把它放在哪里,它仍然显示为可见。

片段忽略可见性参数是否有原因?还有其他非程序化解决方案吗?

我不确定这是否符合您的要求,但您可以在要隐藏的片段上方添加一个虚拟布局容器,并将该布局可见性添加到 gone/visible,无论您想要什么。但是当你想显示片段时,你还应该使片段虚拟父布局可见性可见。

<LinearLayout
    android:id="@+id/dummyLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:visibility="gone"  >
 <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </LinearLayout>

为了将来参考,dora 的答案有效,但有人建议我使用 FrameLayout 而不是 LinearLayout,它更简单并且适用于内部的单个视图:

<FrameLayout
        android:id="@+id/dummyLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

还有另一种方法可行 - 将片段的宽度和高度更改为 0dp,但这被认为是 hack,通常不太正确的解决方案。