如何在没有系统栏的情况下全屏显示启动画面?

问题描述

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

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

注意:我使用的是 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)

    }

}

activity_main:

<?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">

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

enter image description here

在我与屏幕进行任何交互之前,我想要一个没有黑色空间的全屏应用程序,具有完全调整大小的根布局和隐藏系统栏以显示我的启动画面。我该怎么做?

更新:我使用的是 minSdkVersion 22 和 targetSdkVersion 29,并在三星 galaxy A40(Android 10,API 29)上进行了测试。 在三星上,我总是在顶部有一个黑色空间,但在中兴努比亚(API 22)上一切正常。 到目前为止,唯一的解决方案是像这样更改设备设置:How to Enable Full Screen Apps on Galaxy @SlothCoding 在评论中向我展示的内容

解决方法

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

结果:

enter image description here

,

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

<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)
}
,

如果您想删除 actionBar,请尝试以下源代码

getSupportActionbar().hide()

您可以通过编程方式删除 actionBar。 并且,您可以添加此启动画面的全屏模式。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

[duplicate question]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...