如何在SupportMapFragment中使用暗模式?

Tej*_*tel 2 android google-maps supportmapfragment

我在我的代码中使用SupportMapFragment(class ="com.google.android.gms.maps.SupportMapFragment")作为地图.

当我将我的应用程序切换到黑暗或夜晚模式时,除了地图片段之外,一切都在黑暗中.

有没有办法在SupportMapFragment中使用暗模式?

谢谢.

我正在使用光模式地图

在此输入图像描述

我想要下面的夜间模式地图

在此输入图像描述

Afz*_*ivE 11

您可以使用此样式api(夜间模式或您想要的任何其他样式)设置地图样式 https://developers.google.com/maps/documentation/android-api/styling

在拥有GoogleMap对象的类中,您可以使用以下样式:https: //github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/res/raw/mapstyle_night以.json

他们甚至有一个很好的预览编辑器来创建这个json:https: //mapstyle.withgoogle.com/

用法

MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night);
GoogleMap mMap; // assigned in onMapReady()
mMap.setMapStyle(style);
Run Code Online (Sandbox Code Playgroud)


Ole*_*dov -1

您需要View在地图上添加一个具有深色透明背景的:

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/myActivity_mapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <View
        android:id="@+id/myActivity_mapFragmentDark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#55000000"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这样您就可以显示/隐藏此视图以使地图变暗:

mapFragmentDark.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

或者

mapFragmentDark.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)