onCreate()
方法中,在setContentView(R.layout.activity_generic_main);
之后粘贴以下代码。public class GenericMain extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generic_main);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
onCreate
函数中添加以下内容:val window: Window = window
WindowInsetsControllerCompat(window,window.decorView).isAppearanceLightStatusBars = true
resoursces/styles.xml
中添加以下内容。<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">#ffffff</item> <!-- this line sets the status bar color (in my case #ffffff is white) -->
<!-- the following lines are not required -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
这也适用于API级别为21。
对于未来想要在 Android 中使用 Java 编程,以在片段中以编程方式将状态栏颜色从白色更改为主要暗色,并在离开片段时返回到最小 API 21<23的任何人
private void updateStatusBar(boolean isEnter){
Window window = requireActivity().getWindow();
int color = ContextCompat.getColor(requireActivity(),R.color.colorAlertDialog);
if(isEnter) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
else
clearDecorFlags(window);
}
else {
color = ContextCompat.getColor(requireActivity(),R.color.colorPrimaryDark);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
else
clearDecorFlags(window);
}
window.setStatusBarColor(color);
}
private void clearDecorFlags(Window window){
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
所以在 Kotlin 的情况下有些不同
//for Dark status bar icon with white background
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.white))
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv())
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.black))
// for dark background and light theme colours of icon.
解决方法:
当我在暗色主题和亮色主题之间切换时,遇到了一个问题。 以下代码对我起到了作用:
val view = LocalView.current
val window = (view.context as Activity).window
window.statusBarColor = rememberedColors.surface.toArgb() //Some Color
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = isSystemInDarkTheme().not()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
activity?.window?.let {
WindowCompat.setDecorFitsSystemWindows(it, true)
WindowInsetsControllerCompat(it, view).isAppearanceLightStatusBars = true
}
} else { // deprecated method for older versions
var flags: Int = view.systemUiVisibility
flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
view.systemUiVisibility = flags
}