[英]How do I set my status bar transparent but keep by navigation bar black?
正如我的問題所暗示的那樣,我試圖弄清楚如何將我的狀態欄顏色設置為透明,同時保持我的導航欄黑色/自然色(不影響屏幕高度)我提到了這個網站: Android完全透明的狀態欄? 部分有效的解決方案之一是:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
但這會將我的屏幕內容推到導航欄下方,使其透明。 我希望屏幕尺寸保持不變,同時保持導航欄的自然顏色。 只是想讓狀態欄透明,但似乎所有其他方式都會使它變成灰白色,即使我使用( setstatusbarcolor
選項)將其設置為透明。
關於如何去做的任何想法? 我的最小 SDK 版本是 23 。
第二個視圖是添加上面的代碼后的視圖,狀態欄在其中工作,但屏幕在下方滑動,使導航欄也透明(即使設置fitsSystemWindows = false
也無濟於事)。
提前致謝!
嘗試做下面提到的事情可能會對您的示例有所幫助。 如果您仍然遇到問題,請告訴我。 如果它可以解決您的問題,請提供答案。
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);
}
您需要在您的活動主題中添加以下樣式。
在styles.xml
找到您活動的主題並添加以下內容。 由於在您的情況下最低 SDK 版本為 23,因此您無需擔心之前版本的 Android。
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:windowTranslucentStatus">true</item>
這是我得到的截圖。
這是我的主題風格。
<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>
希望能解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.