【问题标题】:How to use dark mode in SupportMapFragment?如何在 SupportMapFragment 中使用暗模式?
【发布时间】:2016-05-10 10:19:33
【问题描述】:

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

当我将我的应用切换到黑暗或夜间模式时,除了地图片段之外,一切都处于黑暗之中。

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

谢谢。

我正在使用光照模式贴图

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

【问题讨论】:

  • 分享你的代码,到目前为止你做了什么
  • 你找到什么了吗?

标签: android google-maps supportmapfragment


【解决方案1】:

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

在您拥有 GoogleMap 对象的类中,您可以使用如下样式: https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/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);

【讨论】:

  • 不幸的是,这仍然会使未加载的图块变成白色 - 这会在片段加载时导致非常不愉快的白色闪烁。
【解决方案2】:

您需要在地图上添加一个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>

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

mapFragmentDark.setVisibility(View.VISIBLE);

mapFragmentDark.setVisibility(View.GONE);

【讨论】:

  • 感谢您给我其他选项来使地图变暗。但我想使用深色地图。我附上了我想要的地图截图。
  • AFAIK android-map 目前不支持此功能。唯一的方法是使用下面的代码使其变暗。
猜你喜欢
  • 2022-01-04
  • 2020-03-07
  • 2020-01-19
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2021-08-24
相关资源
最近更新 更多