地图片段显示为可见,尽管android:visibility=";“走了”;

地图片段显示为可见,尽管android:visibility=";“走了”;,android,android-fragments,visibility,Android,Android Fragments,Visibility,我正在为一个用于较小屏幕的应用程序开发另一种简化布局。这个布局不应该包括在更大的布局中使用的映射片段,但是,如果可能的话,我应该避免编程更改。显示布局的活动也使用其ID处理片段,所以我不能仅仅删除它。我试着让碎片隐形,就像这样: <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:l

我正在为一个用于较小屏幕的应用程序开发另一种简化布局。这个布局不应该包括在更大的布局中使用的映射片段,但是,如果可能的话,我应该避免编程更改。显示布局的活动也使用其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" />

但碎片仍然可见。当我将可见性设置为“不可见”时,也会发生同样的情况。片段的默认位置被视图部分隐藏,但当我在布局中移动片段时,它仍然显示为可见


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

我不确定这是否能满足您的要求,但您可以在要隐藏的片段上方添加一个虚拟布局容器,并将布局可见性添加到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,但这被认为是一种黑客行为,通常不太正确。

您在活动中是否增加了碎片?添加或替换?根据用法,该片段仅由setRetainInstance和getMap使用。我不是100%了解它们是如何工作的,但据我所知,没有任何东西可以控制视图可见性…您是否以编程方式尝试过:
setVisibility(view.GONE)?没有,但如果可能,我必须避免编程更改,特别是因为我只希望它出现在一个版本的布局中。