gpt4 book ai didi

android - 在 Android 10 (API 29) 中隐藏状态栏并在应用程序中使用其空间

转载 作者:行者123 更新时间:2023-12-04 23:48:08 30 4
gpt4 key购买 nike

我正在尝试在 Android 10 中隐藏状态栏。我已经阅读了很多类似问题的答案,但它们不适用于我的设备。目标是将状态栏的空间用作应用程序 View 的一部分:
enter image description here
下面描述的一些解决方案适用于模拟器,但不适用于我的物理设备(Redmi Note 10,API 版本 29)。
我会很感激任何建议。
我试过的:

  • 使用不同的预定义样式,如 @android:style/Theme.NoTitleBar.Fullscreen等等。
  • 使用不同的样式属性,包括:android:windowFullscreen , fitsSystemWindows
  • 使用 在程序代码中设置不同的样式setSystemUiVisibility 包括SYSTEM_UI_FLAG_FULLSCREEN , SYSTEM_UI_FLAG_IMMERSIVE_STICKY , SYSTEM_UI_FLAG_HIDE_NAVIGATION , SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN , SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION , SYSTEM_UI_FLAG_LAYOUT_STABLE .
  • 使用 添加和删除不同的标志清除标志 , 添加标志 设置标志 .
  • 使用 将状态栏颜色设置为透明setStatusBarColor .
  • 使用 请求不同的功能请求窗口功能 .
  • 将布局顶部填充更改为负值。
  • 通过定义 使用插入applyTopWindowInsetsForView onApplyWindowInsets 功能。

  • 我已经设法做到了。
    我设法得到了两个结果,但它们都对我没有好处。
  • 半透明状态栏。

  • enter image description here
  • 完全黑色的状态栏,不占用应用程序 View 的空间。

  • enter image description here
    2022 年 5 月 10 日更新:
    根据以下答案,无法使用 WindowInsetsController在 API 29 或更早版本上: How do I hide the status bar in my Android 11 app?
    解决方案 1
    @Zain 提议.
    Activity :
    protected void onCreate(Bundle savedInstanceState) {
    ...
    WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
    WindowInsetsControllerCompat windowInsetsCompat = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
    windowInsetsCompat.hide(WindowInsetsCompat.Type.statusBars());
    windowInsetsCompat.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    ...
    主题.xml:
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    解决方案 2
    Activity :
    protected void onCreate(Bundle savedInstanceState) {
    ...
    getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    | 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_LOW_PROFILE
    );
    ...
    主题.xml:
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

    最佳答案

    您需要在您的应用程序中边到边显示内容内容(Documentation reference)
    第一步:使用:WindowCompat.setDecorFitsSystemWindows(window, false)

    This is the primary step for ensuring that your app goes edge-to-edge, that is, laid out using the entire width and height of the display. Use WindowCompat.setDecorFitsSystemWindows(window, false) to lay out your app behind the system bars, as shown in the following code example:


    第二步:使用 WindowInsetsControllerCompat 隐藏状态栏:
        WindowInsetsControllerCompat(window, window.decorView).apply {
    // Hide the status bar
    hide(WindowInsetsCompat.Type.statusBars())
    // Allow showing the status bar with swiping from top to bottom
    systemBarsBehavior =
    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
    第三步:从第 1 步开始,底部的 Activity 内容将被底部导航栏遮挡;那么您需要使用 WindowInsetsListener 将 Activity 的根布局的底部边距调整为导航栏的高度:
    Kotlin :
    // root layout of your activity
    val root = findViewById<ConstraintLayout>(R.id.root)
    ViewCompat.setOnApplyWindowInsetsListener(
    root
    ) { view: View, windowInsets: WindowInsetsCompat ->
    val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
    view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply {
    // draw on top of the bottom navigation bar
    bottomMargin = insets.bottom
    }

    // Return CONSUMED if you don't want the window insets to keep being
    // passed down to descendant views.
    WindowInsetsCompat.CONSUMED
    }
    java :
    WindowInsetsControllerCompat windowInsetsCompat = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());

    windowInsetsCompat.hide(WindowInsetsCompat.Type.statusBars());
    windowInsetsCompat.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);

    // root layout of your activity
    ConstraintLayout root = findViewById(R.id.root);

    ViewCompat.setOnApplyWindowInsetsListener(
    root
    , (view, windowInsets) -> {
    Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
    params.bottomMargin = insets.bottom;
    return WindowInsetsCompat.CONSUMED;
    });
    第四步:最后确保在您的应用主题中允许在剪切区域中绘图:
    <item name="android:windowLayoutInDisplayCutoutMode">always</item>

    关于android - 在 Android 10 (API 29) 中隐藏状态栏并在应用程序中使用其空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72179274/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com