繁体   English   中英

如何去除顶部状态栏黑色背景

[英]How to remove top status bar black background

我想创建一个真正的全屏活动,但屏幕顶部总是有一个黑色状态栏。 安卓 9.0。

我已经尝试了几乎所有我能在谷歌和现有应用程序中找到的类似工作。 清单、代码、样式、AS 示例全屏活动,都试过了。

样式.xml:

    <style name="AppThemeA" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

显现:

<activity android:name=".ScreenActivity" android:theme="@style/AppThemeA" />

布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/rootLayout"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Kotlin(注释行已尝试但失败):

class ScreenActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //setTheme(R.style.AppThemeDetector)
        super.onCreate(savedInstanceState)


        //window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        //window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        //window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN

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

        setContentView(R.layout.activity_screen_detector)
        ...
    }
}

预期结果:
预期的

我实际上得到了什么:
实际的

[[[[ 解决方案 ]]]]

找到了造成这种情况的原因。 您应该在设置 -> 显示中将应用程序设置为全屏应用程序。

https://www.gottabemobile.com/how-to-enable-full-screen-apps-on-galaxy-s10

所以固定。 感谢你的帮助。

我在通过凹口或切口区域显示内容时遇到问题。 在文档中找到了这个:

LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - 内容以纵向和横向模式呈现到剪切区域。

对我来说,关键是活动风格中的这一行:

// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 

我想以沉浸式模式显示图像,当我单击它时,系统 UI(状态和导航栏)应该会显示出来。

这是我的完整解决方案:

1- 在 Activity 中显示/隐藏系统 UI 的方法

private fun hideSystemUI() {
    sysUIHidden = true
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
            or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
            )
}


private fun showSystemUI() {
    sysUIHidden = false
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
            )
}

2-确保在您的 xml 布局的根视图中

android:fitsSystemWindows="false"

3- 全屏 Activity 的样式将在状态/导航栏出现时为它们提供半透明背景:

<style name="FullscreenTheme" parent="AppTheme">
    <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowBackground">@null</item>
    <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
    <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    <item name="android:statusBarColor">#50000000</item>
    <item name="android:navigationBarColor">#50000000</item>
    // Important to draw behind cutouts
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 
</style>

<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/sysTransparent</item>
</style>

在您的活动中添加此方法

public static void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

在添加这个之前调用这个super.onCreate(savedInstanceState); onCreate

    hideSystemUI();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    }

如果您的设备有摄像头缺口,则使用上述属性。

除了您的代码之外,您只需在活动中的 setContentView 之前添加此代码

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

这是从Android Q开始实现边到边显示的方法。代码受此博客启发。

首先,在您的styles.xml ,修改AppTheme如下:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        ...
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

假设,我们需要让MainActivity全屏,然后在MainActivity.kt中:

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

此外,请查看此处的文档以启用全屏模式。

所以感谢这个视频,我得到了解决方案。

首先应用程序主题应该扩展一个 NoActionBar 主题

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

之后在 Activity 的 onCreate 方法(在这种情况下使用 viewBinding)应该实现如下。

private lateinit var binding : ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    requestWindowFeature(Window.FEATURE_NO_TITLE)

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

    WindowCompat.setDecorFitsSystemWindows(window, false)

    binding = ActivityMainBinding.inflate(layoutInflater)

    WindowInsetsControllerCompat(window, binding.root).let { controller ->

        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat
            .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

    }

    setContentView(binding.root)

}

这对我有用。 希望它也适合你。

首先,在您的 styles.xml 中,修改 AppTheme,如下所示:

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

并在您的清单文件中提及。

<activity
   android:name=".activities.FullViewActivity"
   android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" 
/>

尝试这个:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
    Window window = mContext.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    int statusBarHeight = (int) dpToPx(24);

    View view = new View(mContext);
    view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
    view.getLayoutParams().height = statusBarHeight;
    ((ViewGroup) window.getDecorView()).addView(view);
     view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}

dpToPx

public float dpToPx(float dp) {
    return (dp * Resources.getSystem().getDisplayMetrics().density);
}

暂无
暂无

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

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