Android:SupportMapFragment 顶部的 Snack bar 显示不正确

标签 android kotlin android-coordinatorlayout supportmapfragment

Activity 中使用的布局(针对 GoogleMap)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/cl_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <fragment
                android:id="@+id/fr_map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".ui.activity.MapActivity" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

在同一个 Activity 中,这就是点击标记时创建 snackbar 的方式。

override fun onMarkerClick(p0: Marker?): Boolean {
    Snackbar.make(findViewById(R.id.cl_map), "Hello World", Snackbar.LENGTH_INDEFINITE).show()
    // finding android.R.id.content does not work either
    // Snackbar.make(findViewById(android.R.id.content), "Hello World", Snackbar.LENGTH_INDEFINITE).show()
    return false
}

以下是屏幕截图和一些症状:
对于标记的初始点击: enter image description here 请注意黑色细线。那是 snackbar 。

但如果我点击 map 主体的任何地方,而不是标记, snackbar 就会变得正常。

enter image description here

此外,如果我将 uiSettings.isMapToolbarEnabled 设置为 false,则默认工具栏会被隐藏,但 snackbar 永远不会出现。无论点击什么,我什至根本看不到黑色细线。

this.googleMap?.uiSettings?.isMapToolbarEnabled = false

一个类似的问题here . 我截图的手机是 Nexus 5。原生 Android 6.0.1,Nexus 5 可以支持的最新操作系统。

最佳答案

根本原因已经找到了。在全屏模式下, snackbar 被 SystemUI 隐藏。可以轻松复制此问题 here .

就我而言,这是导致问题的 fragment

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (hasFocus) hideSystemUI()
}

private fun hideSystemUI() {
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    // or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    // or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    // or View.SYSTEM_UI_FLAG_FULLSCREEN
                    // to hide the nav bar and status bar
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

模式 SYSTEM_UI_FLAG_IMMERSIVESYSTEM_UI_FLAG_IMMERSIVE_STICKY 几乎一直隐藏 snackbar ,如问题正文中所述,无论用户如何点击或在哪里点击。
而第三种全屏模式,没有任何标志,后倾模式 ( GoogleDoc ),仅从第二次点击响应用户有效的 snackbar 。通过显示 SystemUI 的状态栏和导航栏,用户的第一次点击被视为从靠背模式返回。

因此,目前解决此问题的一个快速方法是禁用任一沉浸式标志。但我并不满足于此。将继续寻找更好的答案。

此外,我尝试了 Stackoverflow 上的解决方案,无论是通过设置适当的 z-index 还是 LayoutParam,都无法实现我想要的:在沉浸式界面上显示 snackbar (粘性)全屏。

更新 20191102: 根据 Material Design Guidelines, snackbar 不被认为是显示过多文本的适当方式,也不推荐使用图标。 .我应该依赖的正确 UI 组件是 Bottom Sheet .

关于Android:SupportMapFragment 顶部的 Snack bar 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58653449/

相关文章:

android - 在 Nativescript 中哪里可以找到默认的系统图标?

使用 SAS 上传 Android Azure 存储 Blob; com.microsoft.azure.storage.StorageException : The specified resource does not exist

android - Jetpack composelazycolumn 跳帧(滞后)

kotlin - 函数可以是默认参数值吗?

android - CoordinatorLayout的 'keyline' LayoutParams怎么用?

android - eclipse ,安卓 : Unexpected Unable to execute dex: Multiple dex files define

Android Studio 模拟器无法在 Linux 中的代理服务器后面连接到 Internet

kotlin - Jetpack Compose Row - 绘制子元素 sibling

android - 为什么向上滚动时滚动会停在折叠的工具栏处? (不扩大显示图像)

android - 在 AppBarLayout 折叠之前防止 RecyclerView 在 AppBarLayout 下滚动