Android 如何在没有系统栏的情况下使全屏显示启动画面?
我正在尝试制作一个没有系统栏的全屏作为我的初始屏幕,但是当我的应用程序启动时,我在调整我的根布局时遇到了一些问题 我对底部没有任何问题,因为我的根布局会在导航栏后面调整大小,并转到屏幕的末尾,这是我真正想要做的。 问题在于根布局的顶部,因为它不在屏幕的顶部。在我的屏幕顶部,我有黑色的空白,这是我隐藏状态栏后留下的 注意:Im使用minSdkVersion 22和targetSdkVersion 29 出于演示目的,我将使用一个简单代码,其中包含一个活动和一个对应的XML布局文件,该文件具有绿色背景,约束布局中的两个按钮作为根布局 主要活动:Android 如何在没有系统栏的情况下使全屏显示启动画面?,android,kotlin,splash-screen,android-statusbar,android-navigation-bar,Android,Kotlin,Splash Screen,Android Statusbar,Android Navigation Bar,我正在尝试制作一个没有系统栏的全屏作为我的初始屏幕,但是当我的应用程序启动时,我在调整我的根布局时遇到了一些问题 我对底部没有任何问题,因为我的根布局会在导航栏后面调整大小,并转到屏幕的末尾,这是我真正想要做的。 问题在于根布局的顶部,因为它不在屏幕的顶部。在我的屏幕顶部,我有黑色的空白,这是我隐藏状态栏后留下的 注意:Im使用minSdkVersion 22和targetSdkVersion 29 出于演示目的,我将使用一个简单代码,其中包含一个活动和一个对应的XML布局文件,该文件具有绿色背
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>
在我与屏幕进行任何交互之前,我想要一个没有黑屏的全屏应用程序,并且具有完全调整大小的根布局和隐藏的系统栏。我该怎么做
更新:我使用的是MinsdkVersion22和TargetSDkVersion29,我在三星Galaxy A40(Android 10,API 29)上进行了测试。
在三星,我总是在顶部有一个黑色的空间,但在中兴Nubia(API 22)上,一切正常。
到目前为止,唯一的解决方案是像这样更改设备设置:@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
true//删除状态栏
结果:
用于在应用程序中隐藏状态栏、导航栏以及在用户与屏幕交互时不显示:
首先,转到您的res/主题(在夜间模式下也更改):
如果您想使android设备上的全屏显示大于API 30(android R),只需执行此操作并遵循相同的步骤:
@suppressint(“RestrictedApi”)
趣味createFullScreen(上下文:上下文){
val window=getActivity(上下文)?.window
窗口?申请{
设置标志(
WindowManager.LayoutParams.FLAG_全屏显示,
WindowManager.LayoutParams.FLAG_全屏显示
)
decorView.apply{
systemUiVisibility=View.SYSTEM\u UI\u FLAG\u布局\u稳定或
View.SYSTEM\u UI\u FLAG\u HIDE\u导航或
View.SYSTEM\u UI\u FLAG\u沉浸式\u粘性或
View.SYSTEM\u UI\u FLAG\u全屏或
View.SYSTEM\u UI\u FLAG\u布局\u全屏显示
}
statusBarColor=
getColor(上下文,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);
检查这个问题:我在一台使用API 29的真实设备上进行了测试,似乎只有在设备设置中更改为全屏应用时,这个应用才能全屏工作。我还使用API 22和应用程序进行了测试,在不更改设备设置的情况下全屏显示我的代码。我想知道是否有代码可以在不干扰设备设置的情况下修复此问题?请查看此文档:。。。也检查这个答案,也许这可以做到:你没有看到我把所以我没有行动吧。上面这个蓝色的东西是一个按钮,仅供演示之用。@Dezo我明白了。虽然我写信给你是想说。如果你使用这个主题,那么在任何活动中都不会有actionBar。这就是为什么我写这篇文章只是为了在一个活动中删除它<代码>全屏
模式可能是您的答案。你可以找到你的答案,alsoI将我的android studio升级为API 30,你的代码在API 29的设备上不起作用,但在API 22的设备上起作用。这意味着在带API 29的三星A40上,顶部仍有黑色空间。@Dezo我更新了代码,您只需删除if else即可检查函数createFullScreen()中的API。顶部仍有黑色空间。可能是三星Galaxy Axx手机的问题。它在API 29的设备上不工作,我的顶部仍然有黑色空间。@Dezo检查我的更新图片,它对我非常适用(Android 10)。尝试注释掉您的逻辑代码,然后再次运行。我再次尝试,但在三星galaxy A40(安卓10)上没有工作,因为顶部仍然有一个黑色空格。就目前而言,我认为这只适用于以下情况:也许这适用于其他手机,但不适用于三星galaxy A40
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)
}
getSupportActionbar().hide()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);