要实现Android沉浸式状态栏,可以按照以下步骤进行操作:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
public void hideStatusBar() {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideStatusBar();
}
}
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme">
<!-- ... -->
</activity>
这样就可以实现Android沉浸式状态栏了。重写Activity的onCreate和onWindowFocusChanged方法,分别设置沉浸式状态栏的样式和隐藏状态栏的操作。