【问题标题】:Statusbar of new activities turns white when using transparent statusbar使用透明状态栏时,新活动的状态栏变为白色
【发布时间】:2019-04-26 14:55:11
【问题描述】:

每次我从MainActivity 启动新活动时,新活动的状态栏都会变为白色。出于某种原因,此问题不会影响最初加载的 Activity MainActivity

我意识到这种行为已经在线程 Status bar turns white and does not show content behind it 上进行了足够的讨论,但是建议的大多数解决方案都围绕禁用透明状态栏,我找不到任何其他解决方案适合我。

这是我的一项活动的布局:

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <FrameLayout
        android:id="@+id/search_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

android:fitsSystemWindows="true" 属性在这里似乎没有效果,因为我的活动布局的所有内容都已经适合系统窗口边界。只是状态栏是全白的。

我依赖我的透明状态栏,因此无法禁用它。有没有其他方法可以防止所描述的行为?

【问题讨论】:

    标签: android android-activity android-statusbar


    【解决方案1】:

    我现在已经确定了实现所需行为的两种解决方案:

    1. 在 Activity 的 onCreate 方法中修改窗口标志:
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    

    这会将状态栏着色为灰色,以便系统图标再次可见。

    1. 修改活动根布局的背景颜色以匹配所需的状态栏颜色:
    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark">
    
        <FrameLayout
            android:id="@+id/search_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    感谢Phoenix Wang 的友好评论,我能够找到最后一个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 2017-01-10
      • 2016-10-04
      相关资源
      最近更新 更多