Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式设置google地图片段可见性(API2)_Android_Google Maps_Android Fragments_Visibility - Fatal编程技术网

Android 以编程方式设置google地图片段可见性(API2)

Android 以编程方式设置google地图片段可见性(API2),android,google-maps,android-fragments,visibility,Android,Google Maps,Android Fragments,Visibility,xml: 在Google地图片段中: mFragment = (mFragment) (getSupportFragmentManager().findFragmentById(R.id.mFragment)); mFragment.getView().setVisibility(View.INVISIBLE); 但我如何才能以编程方式设置贴图片段可见性 不能像其他人那样做。简单如下: mMap = ((SupportMapFragment) getSupportFragmentM

xml:

在Google地图片段中:

  mFragment = (mFragment) (getSupportFragmentManager().findFragmentById(R.id.mFragment));
  mFragment.getView().setVisibility(View.INVISIBLE);
但我如何才能以编程方式设置贴图片段可见性

不能像其他人那样做。

简单如下:

  mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
你可以做:

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);
要显示它:

getSupportFragmentManager().beginTransaction().hide(mFragment).commit();

@David
@ferdy182
的答案都是正确的,但他们没有说明上下文

如果以编程方式隐藏/显示片段,则使用
@ferdy182
,如果要隐藏/显示xml格式的片段。您应该遵循
@David

让我解释一下

如果您在xml中有一个单独的frameLayout,并且希望一个接一个地替换该特定片段中的另一个片段。使用此代码添加所有片段。他们将互相依赖

getSupportFragmentManager().beginTransaction().show(mFragment).commit();
代码

//隐藏片段时显示片段的步骤

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);
//隐藏碎片

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .show(fragment1)
          .commit();

只有这对我有用:

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .hide(fragment1)
          .commit();

与在xml中创建
SupportMapFragment
不同,我们可以使用另一种方法,为xml中的
SupportMapFragment
定义容器,然后从类中加载映射

在XML中,假设容器是
FrameLayout
-

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);

您可以尝试将
mMap
转换为
Fragment
并执行相同的
getView().setVisibility(View.INVISIBLE)
,因为
SupportMapFragment
仍然是一个片段。无法从GoogleMap转换为Fragment是否尝试使用
getSupportFragmentManager().beginTransaton().hide(mMap).commit()
getSupportFragmentManager().BeginTransaton().detach(mMap).commit()
已尝试。但是没有起作用。只要地图声明如下:
private GoogleMap mMap我得到:“FragmentTransaction类型中的hide/detach(Fragment)方法不适用于参数(GoogleMap)”,如果我弄错了,请尝试以下方法:
getSupportFragmentManager().BeginTransaton().hide(getSupportFragmentManager().findFragmentById(R.id.mapFragment)).commit()
getSupportFragmentManager().findFragmentById(R.id.mapFragment).getView().setVisibility(View.INVISIBLE)这帮助我了解了如何通过编程更改支持图片段的高度。谢谢如果地图在滚动视图中,当我隐藏滚动视图时,地图不会隐藏。如果我在Scrollview之前隐藏地图,它仍然不会隐藏。我必须隐藏地图,等待1毫秒,然后使用计时器隐藏滚动视图。你不应该通过更改其可见性来隐藏它。隐藏和显示也适用于xml中定义的片段,您只需执行findFragmentById即可将片段实例传递给片段事务
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .show(fragment1)
          .commit();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .hide(fragment1)
          .commit();
View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);
    <FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
   private void showMap() {
   mMapContainer.setVisibility(View.VISIBLE);
   mSupportMapFragment = SupportMapFragment.newInstance();
   getChildFragmentManager().beginTransaction()
                         .replace(R.id.mapContainer, mSupportMapFragment).commit();
   }

    private void hideMap() {
        mMapContainer.setVisibility(View.VISIBLE);
        if (mSupportMapFragment != null) {
            getChildFragmentManager().beginTransaction()
                                 .detach(mSupportMapFragment).commit();
        }
    }