要實現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方法,分別設置沉浸式狀態欄的樣式和隱藏狀態欄的操作。