【问题标题】:How to remove top status bar black background如何去除顶部状态栏黑色背景
【发布时间】:2019-10-14 15:52:39
【问题描述】:

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

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

styles.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)
        ...
    }
}

预期结果:

我实际得到的:

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

找到造成这种情况的原因。您应该在 Settings -> Display 中将 App 设置为 FullScreen App。

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

如此固定。感谢您的所有帮助。

【问题讨论】:

标签: android fullscreen statusbar appcompatactivity


【解决方案1】:

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

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>

【讨论】:

    【解决方案2】:

    在您的活动中添加此方法

    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;
        }
    

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

    【讨论】:

    • 这应该是公认的答案。没有其他东西对我有用。
    • 这很简单也很有效,没有其他东西对我有用。
    【解决方案3】:

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

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    【讨论】:

      【解决方案4】:

      这是一种从Android Q开始实现边到边显示的方法。代码的灵感来自blog

      首先,在你的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
                      )
      

      另外,请查看文档 here 以启用全屏模式。

      【讨论】:

      • 试过了,失败了。三星 S10+。
      • 您使用的是哪个 Android 版本?
      • 三星S10+,安卓派
      【解决方案5】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多