Android R隐藏状态栏->向下移动视图并出现一个黑色矩形

Posted

技术标签:

【中文标题】Android R隐藏状态栏->向下移动视图并出现一个黑色矩形【英文标题】:Android R hide statusbar -> move view down and a black rectangle appears 【发布时间】:2021-02-12 03:28:14 【问题描述】:

android R + Pixel 4 Emulator:当我隐藏状态栏时,主视图向下移动,顶部出现一个黑色方块(与状态栏大小相同)。 它在 Android 11 之前工作。 看起来很简单,但我找不到解决方案……而且我需要全屏模式。

我尝试使用 SYSTEM_UI_FLAG_FULLSCREEN(现已弃用)和 controller.hide(WindowInsets.Type.statusBars()); 这两种方式有同样的问题。

这里是主要代码: MainActivity.java

public class MainActivity extends AppCompatActivity 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Button buttonShow = findViewById(R.id.b1);
    buttonShow.setOnClickListener(new View.OnClickListener() 
        public void onClick(View v) 
           showBars();
        
    );

    Button buttonHide = findViewById(R.id.b2);
    buttonHide.setOnClickListener(new View.OnClickListener() 
        public void onClick(View v) 
          hideBars();
        
    );

    showBars();


public void showBars()

    View decorView = getWindow().getDecorView();
    int uiOptions =  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

    decorView.setSystemUiVisibility(uiOptions);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    // New code For Android 11, same effect...
    /*
    WindowInsetsController controller = getWindow().getInsetsController();
    if(controller != null) 
        controller.show(WindowInsets.Type.navigationBars());
        controller.show(WindowInsets.Type.statusBars());
        controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    */


public void hideBars()


    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE;


    decorView.setSystemUiVisibility(uiOptions);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    // New code For Android 11, same effect...
    /* WindowInsetsController controller = getWindow().getInsetsController();
      if(controller != null) 
          controller.hide(WindowInsets.Type.navigationBars());
          controller.hide(WindowInsets.Type.statusBars());
          controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
      */


activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_
    android:layout_
    tools:context=".MainActivity"
    android:background="@color/purple_200"
    android:id="@+id/layoutMain">


    <Button
        android:id="@+id/b1"
        android:layout_marginTop="200dp"
        android:layout_
        android:layout_
        android:text="show" />

    <Button
        android:id="@+id/b2"
        android:layout_marginTop="100dp"
        android:layout_
        android:layout_
        android:text="hide"
/>

</RelativeLayout>

【问题讨论】:

你必须应用这些代码***.com/questions/43511326/… 【参考方案1】:

这对我有用: 使用 v27 配置为您想要的全屏活动创建样式资源文件,而无需剪切:

<style name="Theme.MyApp.ActivityFullscreenNoCutout">
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

并将样式分配给清单中的活动:

<activity android:name=".MyActivity"
        android:theme="@style/Theme.MyApp.ActivityFullscreenNoCutout"/>

链接:https://developer.android.com/guide/topics/display-cutout

如果您有一个操作/应用栏,则必须将工具栏添加到您的活动中,而不是使用默认工具栏。

链接:https://developer.android.com/training/appbar/setting-up

【讨论】:

非常感谢。我将它直接放入主题并且工作正常:shortEdges【参考方案2】:

试试这个代码 sn-p。

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
    

【讨论】:

【参考方案3】:

活动代码中的某处...

    @TargetApi(Build.VERSION_CODES.R) 
void hideStatusBar_with_API30() 
    View decorView = getWindow().getDecorView();
    // Order swiped status bar to appear semitransparent.
    // If you do not that
    //  then once appeared bar will not disappear after nor few seconds nor you gesture.
    decorView.getWindowInsetsController().setSystemBarsBehavior(
            WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    // Hide the status bar.
    decorView.getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    // The status bar is allowed to appear with a swipe gesture.
    // When it is then app layout is resized.
    // To prevent changing the layout, you need to add:
    getWindow().setDecorFitsSystemWindows(false);
    // The status bar will appear over stable layout

【讨论】:

以上是关于Android R隐藏状态栏->向下移动视图并出现一个黑色矩形的主要内容,如果未能解决你的问题,请参考以下文章

状态栏整个视图向下移动 - iOS 11

视图框架不向下移动 20px(状态栏的高度)

Xamarin表示Android导航栏向下移动

在 iPad 缩放模式下隐藏状态栏会向下推窗口

如何移动子视图控制器,同时使用自动布局而不是框架隐藏状态栏?

如何在 segue 动画之前强制布局?