View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
new ToastUtil(mContext).showToast("11111");
}else {
new ToastUtil(mContext).showToast("22222");
}
}
});
最優(yōu)解
private static Handler sHandler;
Runnable mHideRunnable = () -> {
int flags;
int curApiVersion = Build.VERSION.SDK_INT;
// This work only for android 4.4+
if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
// hide navigation bar permanently in android activity
// touch the screen, the navigation bar will not show
flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_FULLSCREEN;
} else {
// touch the screen, the navigation bar will show
flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// must be executed in main thread
getWindow().getDecorView().setSystemUiVisibility(flags);
};
sHandler = new Handler();
sHandler.post(mHideRunnable); // hide the navigation bar
decorView.setOnSystemUiVisibilityChangeListener(visibility -> {
sHandler.post(mHideRunnable); // hide the navigation bar
if (visibility == 0) {
lp.height = DisplayUtil.getScreenH(mContext) - titleHeight - DisplayUtil.getNavigationBarHeight(mContext);
} else {
lp.height = DisplayUtil.getScreenH(mContext) - titleHeight;
}
window.setAttributes(lp);
});