繁体   English   中英

如何去除底部药丸手势栏 Android 下方的空间?

[英]How to remove the space under the bottom pill gesture bar Android?

我如何摆脱底部“手势栏”下的这个微小的黑色空间? 大多数应用程序一直延伸到底部,但我仍然在底部看到这个小黑条。

主要活动 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="com.myProject.fragments.SignUpFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/sign_up_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图像描述

据我所知,您无法删除此栏,但可以使其透明

这里有一些代码

方法一

this.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    

因为这会让你的屏幕充满并且透明它有点hack但你可以试试这个

这里上面的代码确实使您的屏幕全屏并且手势将是透明的,您将得到您想要的 output

方法二

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

在你的风格改变这样

<resources>
<style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

在你想要全屏到主布局的活动中给出这个标志

android:fitsSystemWindows="true"

方法三

此代码不是我的,我是从某个网站上获取的,我忘记了从哪里获取的,但请尽快提供参考

这里有一个使透明化的功能

fun Activity.transparentStatusAndNavigation(
    systemUiScrim: Int = Color.parseColor("#40000000") // 25% black
) {
    var systemUiVisibility = 0
    // Use a dark scrim by default since light status is API 23+
    var statusBarColor = systemUiScrim
    //  Use a dark scrim by default since light nav bar is API 27+
    var navigationBarColor = systemUiScrim
    val winParams = window.attributes


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        statusBarColor = Color.TRANSPARENT
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
        navigationBarColor = Color.TRANSPARENT
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        systemUiVisibility = systemUiVisibility or
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        window.decorView.systemUiVisibility = systemUiVisibility
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        winParams.flags = winParams.flags or
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        winParams.flags = winParams.flags and
                (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION).inv()
        window.statusBarColor = statusBarColor
        window.navigationBarColor = navigationBarColor
    }

    window.attributes = winParams
}

希望这可以帮助

我如何摆脱底部“手势栏”下的这个微小的黑色空间?

要将应用程序扩展到最底部,您需要使您的应用程序全屏显示

WindowCompat.setDecorFitsSystemWindows(window, false)

但这也会将应用程序绘制在顶部状态栏上; 为了避免您需要通过注册OnApplyWindowInsetsListener使用插图来处理重叠

首先给你的根布局ConstraintLayout一个 id,比如root

val root = findViewById<ConstraintLayout>(R.id.root)
val onApplyWindowInsetsListener =
    OnApplyWindowInsetsListener { view: View, windowInsets: WindowInsetsCompat ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        
        // Apply the insets as a margin to the root view in order to set the layout drawing area
        view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply {
            topMargin = insets.top // to draw below the top status bar
            leftMargin = insets.left
            rightMargin = insets.right
        }

        // Return CONSUMED if you don't want want the window insets to keep being
        // passed down to descendant views.
        WindowInsetsCompat.CONSUMED
    }
ViewCompat.setOnApplyWindowInsetsListener(root, onApplyWindowInsetsListener)

现在您需要隐藏底部导航栏,并在用户从底部向上滑动时显示它:

WindowInsetsControllerCompat(window, window.decorView).apply {
    hide(WindowInsetsCompat.Type.navigationBars())
    systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

这是从 API 级别 21 开始工作的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM