如何将状态栏设置为透明,但导航栏保持黑色?

时间:2019-05-01 14:19:52

标签: java android android-layout android-statusbar android-navigation-bar

正如我的问题所建议的,我试图弄清楚如何将状态栏颜色设置为透明,同时保持导航栏为黑色/自然色(不影响屏幕高度) 我提到这个网站: Android Completely transparent Status Bar? 部分有效的解决方案之一是:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

但是将我的屏幕内容推到导航栏下方,使其透明。我希望屏幕尺寸保持不变,同时保持导航栏的自然色。只是想使状态栏透明,但是,即使我使用(setstatusbarcolor选项将其设置为透明,似乎所有其他方式都将其变为白色。

关于如何进行操作的任何想法?我的最低SDK版本是23

The first view without doing anything

第二个视图是在上面添加了状态栏所在位置的代码之后的视图,但屏幕仍在下面滑动,使导航栏也变得透明(即使设置fitsSystemWindows = false也无济于事)。

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要在活动主题中添加以下样式。

styles.xml中找到您的活动主题并添加以下内容。由于您的最低SDK版本为23,因此您无需担心Android的先前版本。

<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:windowTranslucentStatus">true</item>

这是我所拥有的屏幕截图。

enter image description here

这是我的情况下的主题样式。

<style name="NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

希望能解决您的问题。

答案 1 :(得分:0)

尝试执行以下提到的操作可能会对您的示例有所帮助。如果您仍然遇到问题,请让我。如果可以解决您的问题,请提供答案。

AndroidManifest.xml

<activity
   ....            
   android:theme="@style/TranslucentStatusBarTheme">
</activity>

values-v23\styles.xml

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

    <style name="TranslucentStatusBarTheme" parent="AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>
</resources>

Activity.java

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Check if the version of Android is Marshmallow or higher
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int newUiOptions = getWindow().getDecorView().getSystemUiVisibility();
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        getWindow().getDecorView().setSystemUiVisibility(newUiOptions
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    // Check if the version of Android is Lollipop or higher
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        setWindowFlag(this, false);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorTranslucent));
    }

    super.onCreate(savedInstanceState);
}