0

我正在尝试制作一个没有系统栏的全屏作为我的启动屏幕,但是在我的应用程序启动时调整我的根布局大小时遇到​​了一些问题。

我对底部没有任何问题,因为我的根布局在导航栏后面调整大小并转到屏幕末尾我真正想做的事情。问题出在根布局的顶部,因为它没有到达屏幕顶部。在我的屏幕顶部,我隐藏状态栏后留下了黑色的空白区域。

注意:我使用 minSdkVersion 22 和 targetSdkVersion 29。

出于演示目的,我将使用简单的代码,其中包含一个活动和一个对应的 XML 布局文件,其中绿色背景和约束布局中的 2 个按钮作为根布局。

主要活动:

package com.systemwindowexercise

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        window.navigationBarColor = resources.getColor(R.color.translucent)
        window.statusBarColor = resources.getColor(R.color.translucent)

        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)

    }

}

活动主:

<?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:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

主题:

<style name="Theme.SystemWindowExercise" parent="Theme.MaterialComponents.DayNight.NoActionBar">

左边是我在没有系统栏的情况下用黑色空间触摸屏幕之前的应用程序,但调整大小的根布局(绿色)错误,右边是我用系统栏触摸屏幕并正确调整大小的根布局后的应用程序。

在此处输入图像描述

在我与屏幕进行任何交互之前,我想要一个全屏的应用程序,没有黑色空间,并且具有完全调整大小的根布局并为我的初始屏幕隐藏系统栏。我怎样才能做到这一点?

更新:我正在使用 minSdkVersion 22 和 targetSdkVersion 29,并在三星 Galaxy A40(Android 10,API 29)上进行了测试。在三星上,我总是在顶部有一个黑色空间,但在 ZTE Nubia (API 22) 上一切正常。到目前为止,唯一的解决方案是像这样更改设备设置:如何在 Galaxy 上启用全屏应用程序@SlothCoding 在评论中向我展示的内容。

UPDATE2:如果您不想更改设备设置,那么您需要此代码用于在屏幕区域内置摄像头的手机,或者在官方术语中,您需要它在剪切区域中显示内容:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    }
4

2 回答 2

0
<style name="Theme.xxx" parent="Theme.MaterialComponents.DayNight.NoActionBar">    //no actionbar, but status bar exists
    <item name="android:windowFullscreen">true</item>                       //remove status bar

结果:

在此处输入图像描述

于 2021-01-03T22:51:56.390 回答
0

要在应用中隐藏状态栏、导航栏以及在用户与屏幕交互时不显示:
首先,转到您的 res/theme(在夜间模式下也可以更改):

<style name="Theme.xxx" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
        <!-- Customize your theme here. -->
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
</style>

接下来,进入你的第一个屏幕启动活动(我的是主活动),创建这个函数:

@SuppressLint("RestrictedApi")
fun createFullScreen(context: Context) {
        val window = getActivity(context)?.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window?.apply {
                setDecorFitsSystemWindows(false)
            }
        } else {
            window?.apply {
                setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
                )
                decorView.apply {
                    systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                            View.SYSTEM_UI_FLAG_FULLSCREEN or 
                            
                }
                statusBarColor =
                    ContextCompat.getColor(context, android.R.color.transparent)
            }
        }
    }

如果您想在 API 30 (Android R) 以上的 android 设备上制作全屏,只需执行此操作并按照相同的步骤操作:

@SuppressLint("RestrictedApi")
fun createFullScreen(context: Context) {
        val window = getActivity(context)?.window

        window?.apply {
            setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
            decorView.apply {
                systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            }
            statusBarColor =
                ContextCompat.getColor(context, android.R.color.transparent)
        }
    }

最后,只需将其放入 onCreate() 函数中,如下所示:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var adapterViewPager: PosterViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
//      Here for your called function
        createFullScreen(this@MainActivity)
}
于 2021-01-04T04:56:49.553 回答