我不确定您要针对哪个API级别进行目标,但如果您可以使用API 23特定的内容,您可以将以下内容添加到您的AppTheme styles.xml中:
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>
当 android:windowLightStatusBar
设置为 true 时,状态栏文本颜色将在状态栏颜色为白色时可见,反之亦然。当 android:windowLightStatusBar
设置为 false 时,状态栏文本颜色将被设计成在状态栏颜色为深色时可见。
示例:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Status bar stuff. -->
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>
</style>
您可以通过编程来完成这个操作,像这样 答案
只需添加此内容
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
兼容版本适用于API 23及以上版本。
这是它的代码:
// deprecated
// WindowInsetsControllerCompat(window, view).isAppearanceLightStatusBars = Boolean
// also deprecated
// ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = Boolean
WindowCompat.getInsetsController(window, decorView)?.isAppearanceLightStatusBars = Boolean
您可以直接从Activity
获取window
。
我喜欢将其添加到Window
扩展方法中:
fun Window.setLightStatusBars(b: Boolean) {
WindowCompat.getInsetsController(this, decorView)?.isAppearanceLightStatusBars = b
}
你需要androidx.core
来实现此功能
非常简单:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);// set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this,R.color.white));// set status background white
反之亦然:
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this, R.color.black));
View decorView = getWindow().getDecorView(); //set status background black
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); //set status text light
跟随 @Jon 的回答,我会在新的API上稍微更新一下。对于新的API和主题以及夜间模式(暗黑模式),我会通过添加 v23/styles.xml 并在那里设置状态栏背景和文字颜色来实现:
<item name="android:statusBarColor">@color/lightColor</item>
<item name="android:windowLightStatusBar">true</item>
而在night/styles.xml文件中:
<item name="android:statusBarColor" tools:targetApi="l">@color/darkColor</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
默认的styles.xml不会包含任何此代码,或仅包含此代码,但记得不要将其设置为light:
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
这样,我们为状态栏设置了浅色背景(和文本颜色),但仅适用于API版本为23及以上的设备。在API版本小于23的设备上,背景将不会更改,因为我认为这不是我们想要的,而文本颜色将保持为白色。
暗色主题在API 29中添加,因此我们不必担心API 21上的暗色主题 ;)
然而,缺点是我们又添加了一个需要记住管理的文件。和之前一样,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 在我的情况下起到了作用,请不要忘记设置 API 版本高于 22。
在 setContentView 后面添加以下内容:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);// set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimaryDark));// set status background white
对我来说它有效
从 API 21+ 开始,这对我有效:
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetsController.setAppearanceLightStatusBars(true);
我使用
Theme.AppCompat.DayNight
这段代码适用于白天和夜晚模式:
getWindow().setStatusBarColor(getWindow().getNavigationBarColor());
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO)
windowInsetsController.setAppearanceLightStatusBars(true);
else
windowInsetsController.setAppearanceLightStatusBars(false);
如果不是闪屏页面,请尝试此方法
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setNavigationBarColor(ContextCompat.getColor(context, R.color.white));
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.white));
onCreate()
方法中,粘贴以下代码。"最初的回答"try {
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_red));
}
} catch (Exception e) {
e.printStackTrace();
}