【问题标题】:How to have transparent navigation bar while having views above it and behind it?如何在导航栏上方和后方有视图的同时拥有透明导航栏?
【发布时间】:2018-11-26 12:06:52
【问题描述】:

背景

可以将导航栏设置为透明,以便在其后面绘制内容(在 Z 坐标中),并且在其上方(在 Y 坐标中)会有视图,就像在相机应用上一样:

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

问题

我发现的每一个解决方案(还有很多,例如 here)都不允许我选择既拥有透明导航栏,又拥有其上方和背后的视图。

我尝试过的

这是我尝试解决的一种方法,使用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>

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

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

/**
 * 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. 如何在导航栏上方创建一个视图,在其后创建一个视图,例如在相机应用程序上?我也不希望在旧的 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>

【问题讨论】:

  • 澄清一下,“在导航栏上方”,您的意思是直接遮蔽后退/主页/窗口控件吗?或者只是在更大的 z-index 上但不直接覆盖?
  • "above" 在 Y 坐标中。 “后面”在 Z 坐标中。将更新问题以使其清楚。

标签: android android-navigation-bar


【解决方案1】:

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

【讨论】:

  • 我不想要它的颜色。我想让它透明,并且能够在它的后面和上面看到。
  • 相机应用不只是导航栏的颜色。它显示了它背后的视频预览。
【解决方案2】:

为了做到这一点,你需要

将视图背景属性设置为 null 或 Color.TRANSPARENT

颜色的 setAlpha 为 0x00FFFFFF

并像 view.setBackground(new ColorDrawable(Color.TRANSPARENT)); 一样应用于背景;

【讨论】:

  • 什么?如果仅与颜色有关,这对视图的位置有何帮助?
猜你喜欢
  • 2021-11-21
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
相关资源
最近更新 更多