打开导航栏时如何改变状态栏颜色

时间:2018-11-30 13:44:42

标签: android xml

我已经创建了一个导航查看器,但是当您打开它时,状态栏中的颜色不会改变。这是发生了什么: no color change

这就是我想要的结果: color change

您看到在第一张图像中状态栏保持不变,而在第二张图像中状态栏发生了变化。我现在为此拥有的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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UI.Story.MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include
                    layout="@layout/app_bar"/>

                <FrameLayout
                    android:id="@+id/main_frame"
                    android:layout_weight="2"
                    android:layout_width="match_parent"
                    android:layout_height="0px"/>

            </LinearLayout>
        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navigation_drawer"
            app:menu="@menu/menu" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

它有效,但是当我将Theme.AppCompat.Light.NoActionBar更改为Theme.MaterialComponents.Light.NoActionBar时,它不起作用

1 个答案:

答案 0 :(得分:0)

我过去曾使用此代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    //make translucent statusBar on kitkat devices
    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
    }
    if (Build.VERSION.SDK_INT >= 19) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    //make fully Android Transparent Status bar
    if (Build.VERSION.SDK_INT >= 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
}

public static void setWindowFlag(Activity activity, final int bits, boolean on) {
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}

source