【问题标题】:Android: Translucent status bar not working with Google Maps APIAndroid:半透明状态栏不适用于 Google Maps API
【发布时间】:2017-06-01 12:07:38
【问题描述】:

我有一个问题,状态栏不会变成半透明。半透明状态栏和导航栏代码本身可以正常工作,但是,当我将 Google Maps Fragment 实施到活动视图中时,半透明栏变成灰色。我已经用一个完全透明的状态栏对此进行了测试,但它首先再次起作用,然后在添加地图后具有白色背景。似乎谷歌地图有某种覆盖。感谢任何帮助,以使状态栏使用 Google Maps API 变为半透明。

XML 文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.mapwithmarker.MapsMarkerActivity"
        tools:layout="@layout/activity_main" />

</android.support.v4.widget.DrawerLayout>

半透明状态栏

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

【问题讨论】:

    标签: java android google-maps-android-api-2 android-xml android-statusbar


    【解决方案1】:

    我找到了解决方案。如果有人在抽屉布局中添加地图,请将 DrawerLayout 中的 android:fitsSystemWindows 设置为 "false"

    【讨论】:

    • 这确实使地图在状态栏下绘制,但问题在于指南针 UI 和我的位置按钮 UI,它们被推到工具栏下。关于如何避免这种情况的任何想法?
    • 您可以在onMapReady 中使用googleMap.setPadding(); 来下推地图对象。
    【解决方案2】:

    如果您在 Activity 中添加地图,则在您的 onCreate() 方法中编写以下代码行:

        window.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    statusBarColor = Color.TRANSPARENT
            }
        }
    

    如果在 Fragment 中添加地图,则在 onCreate() 方法中添加以下代码行:

        activity!!.window.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    statusBarColor = Color.TRANSPARENT
            }
        }
    

    【讨论】:

      【解决方案3】:

      fitsSystemWindows 设置为false 对我不起作用

      然而,负边距成功了

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:showIn="@layout/app_bar_main"
          android:id="@+id/fragment_container"
          android:fitsSystemWindows="true"
          android:padding="0dp">
          <fragment
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:map="http://schemas.android.com/apk/res-auto"
              android:id="@+id/map"
              android:name="com.google.android.gms.maps.MapFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="-25dp"
              map:uiCompass="false"
              android:fitsSystemWindows="true" />
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 2017-07-07
        • 1970-01-01
        • 2011-02-22
        • 2015-04-02
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多