android - 如何在导航栏上方和后面有 View 的同时拥有透明的导航栏?

标签 android android-navigation-bar

背景

导航栏可以是透明的,这样内容就会被绘制在它的后面(在 Z 坐标中)并且它上面会有 View (在 Y 坐标中),就像在相机应用上一样:

enter image description here

对于相机应用,导航栏后面有内容(相机预览),上面有 View (按钮)

问题

我找到的每个解决方案(还有很多,例如 here )都不允许我选择既有透明导航栏,又有 View 在其上方和后面。

我尝试过的

这是我尝试使用 android:fitsSystemWindows="true"(样本 here)解决它的一种方法:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        setContentView(R.layout.activity_main)
    }
}

activity_main.xml

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:background="#33ff0000"
              android:text="text behind nav bar"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintStart_toStartOf="parent"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:background="#3300ff00"
              android:text="text above nav bar" android:fitsSystemWindows="true"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>

但结果是两个 View 都在导航栏后面:

enter image description here

我发现唯一可行的方法是获取导航栏的高度,并将 View 的底部边距设置为该值,但这似乎是一个奇怪的解决方案,我喜欢尽量避免:

/**
 * returns the natural orientation of the device: Configuration.ORIENTATION_LANDSCAPE or Configuration.ORIENTATION_PORTRAIT .<br></br>
 * The result should be consistent no matter the orientation of the device
 */
fun getScreenNaturalOrientation(context: Context): Int {
    with(context) {
        //based on : http://stackoverflow.com/a/9888357/878126
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val config = resources.configuration
        val rotation = windowManager.defaultDisplay.rotation
        return if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)
            Configuration.ORIENTATION_LANDSCAPE
        else
            Configuration.ORIENTATION_PORTRAIT
    }
}

fun getNavBarHeight(context: Context, defaultHeightToReturn: Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48f, context.resources.displayMetrics).toInt()): Int {
    val resources = context.resources
    val orientation = resources.configuration.orientation
    val isTablet = getScreenNaturalOrientation(context) == Configuration.ORIENTATION_LANDSCAPE
    val resourceId = if (isTablet)
        resources.getIdentifier(if (orientation == Configuration.ORIENTATION_PORTRAIT) "navigation_bar_height" else "navigation_bar_height_landscape", "dimen", "android")
    else
        resources.getIdentifier(if (orientation == Configuration.ORIENTATION_PORTRAIT) "navigation_bar_height" else "navigation_bar_width", "dimen", "android")
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId)
    }
    return defaultHeightToReturn
}

问题

  1. 它怎么会忽略 android:fitsSystemWindows

  2. 如何在导航栏上方制作一个 View ,在导航栏后面制作一个 View ,就像在相机应用上一样?我也不希望在旧的 Android 版本上无缘无故地有额外的空间。


编辑:关于为什么 android:fitsSystemWindows 不起作用,我找到了这个和这个。但是,当我尝试通过仅使用 CoordinatorLayout 来使用此技术时,它在上述情况下仍然不起作用:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 tools:context=".MainActivity" android:layout_width="match_parent"
                                                 android:layout_height="match_parent">
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:background="#33ff0000"
              android:text="text behind nav bar" android:textSize="20sp" android:layout_gravity="bottom"
    />

    <TextView android:layout_width="wrap_content" android:textSize="20sp" android:layout_gravity="bottom|end"
              android:layout_height="wrap_content" android:background="#3300ff00"
              android:text="text above nav bar" android:fitsSystemWindows="true"
    />

</android.support.design.widget.CoordinatorLayout>

最佳答案

您可以使导航栏的背景与您的 Activity 的背景相同,从而使其显示为透明。

关于android - 如何在导航栏上方和后面有 View 的同时拥有透明的导航栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480773/

相关文章:

android - RewardedVideoAd.loadAd() 的网络开销是多少?

安卓:windowLightNavigationBar 不工作

Android:全屏微调下拉菜单

android - 长按删除项目

android - FireMonkey TControl.MakeScreenshot 在移动平台上生成尺寸过小的位图

java - 在自定义 ListView 适配器中创建方法的正确方法

java - Android中的 "Top-level destination"是什么?

android - 如何在 android jetpack compose 的非全屏横向模式下使用背景颜色为切口/缺口区域着色

android - 为什么 BottomSheetDialog 横向绘制在导航栏下方?

安卓绑定(bind)库