我正在尝试制作一个没有系统栏的全屏作为我的启动屏幕,但是在我的应用程序启动时调整我的根布局大小时遇到了一些问题。
我对底部没有任何问题,因为我的根布局在导航栏后面调整大小并转到屏幕末尾我真正想做的事情。问题出在根布局的顶部,因为它没有到达屏幕顶部。在我的屏幕顶部,我隐藏状态栏后留下了黑色的空白区域。
注意:我使用 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
}