【问题标题】:How to make the status bar completely transparent with dark text color with Kotlin如何使用 Kotlin 使用深色文本颜色使状态栏完全透明
【发布时间】:2020-04-09 23:24:49
【问题描述】:

我使用 Kotlin 创建一个 android 应用程序,我想知道如何使用 Kotlin 使状态栏完全透明并带有深色文本 我看到了以下解决方案,但对我不起作用 Dark-text transparent status bar with opaque navigation bar 这是我的以下代码:

   if (VERSION.SDK_INT in 19..20) {
            setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
        }
        if (VERSION.SDK_INT >= 19) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        if (VERSION.SDK_INT >= 21) {
            setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
            window.statusBarColor = Color.TRANSPARENT
        }

这是 style.xml 中的自定义样式:

<style name="MyStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:navigationBarColor">@android:color/black</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

还有我的 xml 代码:

<..... android:fitssystemwindows="true" ....>

 <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="189dp"
        app:elevation="0dp"
        android:theme="@style/MyStyle" ....>

我想知道我的代码中的问题在哪里以及如何纠正它以使状态栏完全透明且文本颜色为深色

【问题讨论】:

  • 从 API 23 你可以设置View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,尝试添加到systemUiVisibility
  • 我试试,不行

标签: android kotlin android-statusbar


【解决方案1】:

你必须遵循三个步骤。

  1. 更新你的 style.xml 和主题

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    
  2. 在你的主xml中添加

    android:fitsSystemWindows="true"
    
  3. setcontView之前的活动中添加

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    

【讨论】:

  • 是的,比你,它现在可以工作但是导航栏现在隐藏了,如何显示它
猜你喜欢
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2023-01-13
  • 2023-03-29
  • 2011-10-04
  • 2015-09-08
  • 1970-01-01
相关资源
最近更新 更多