问题
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? 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).
Any ideas on how to go about it? My min SDK version is 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).
Thanks in advance!
回答1:
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. As the minimum SDK version is 23 in your case, you do not need to worry about the previous versions of 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.
回答2:
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);
}
来源:https://stackoverflow.com/questions/55937736/how-do-i-set-my-status-bar-transparent-but-keep-by-navigation-bar-black