简体   繁体   English

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

[英]How do I set my status bar transparent but keep by navigation bar black?

As my question suggests, I am trying to figure out how to set my status bar color transparent while keeping my navigation bar black/natural color (without affecting the screen height) I referred to this site : Android Completely transparent Status Bar?正如我的问题所暗示的那样,我试图弄清楚如何将我的状态栏颜色设置为透明,同时保持我的导航栏黑色/自然色(不影响屏幕高度)我提到了这个网站: Android完全透明的状态栏? one of the solutions that partially worked was :部分有效的解决方案之一是:

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

but that pushes my screen content below navigation bar making it transparent.但这会将我的屏幕内容推到导航栏下方,使其透明。 I want the screen size to remain the same while keeping the navigation bar its natural color.我希望屏幕尺寸保持不变,同时保持导航栏的自然颜色。 Just want to make the status bar transparent, but seems like all other ways make it off-white even when I set it transparent with ( setstatusbarcolor option).只是想让状态栏透明,但似乎所有其他方式都会使它变成灰白色,即使我使用( setstatusbarcolor选项)将其设置为透明。

Any ideas on how to go about it?关于如何去做的任何想法? My min SDK version is 23 .我的最小 SDK 版本是 23

不做任何事情的第一个视图

The second view is the view after adding the code above where the status bar works but the screen slides below making the nav bar transparent as well (even setting fitsSystemWindows = false doesn't help).第二个视图是添加上面的代码后的视图,状态栏在其中工作,但屏幕在下方滑动,使导航栏也透明(即使设置fitsSystemWindows = false也无济于事)。

在此处输入图片说明

Thanks in advance!提前致谢!

Try to do below mentioned things may help you in your example.尝试做下面提到的事情可能会对您的示例有所帮助。 Let me if you still facing issue.如果您仍然遇到问题,请告诉我。 If it will solve your issue then please provided answer.如果它可以解决您的问题,请提供答案。

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);
}

You need to add the following style in your activity theme.您需要在您的活动主题中添加以下样式。

In the styles.xml find the theme of your activity and add the following.styles.xml找到您活动的主题并添加以下内容。 As the minimum SDK version is 23 in your case, you do not need to worry about the previous versions of Android.由于在您的情况下最低 SDK 版本为 23,因此您无需担心之前版本的 Android。

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

Here is the screenshot of what I have got.这是我得到的截图。

在此处输入图片说明

Here's the theme style that I have in my case.这是我的主题风格。

<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>

Hope that solves your problem.希望能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM