android sdk 19以上的手机存在虚拟按键,玩游戏本着屏越大越舒服的感觉,因此在游戏刚开始的时候自动隐藏的虚拟按键,对用户来说是很友好的做法,代码如下:
void onCreate(Bundle savedInstanceState) {
{
super.onCreate(savedInstanceState);
initImmersiveMode();
}
@SuppressLint("NewApi")
private void initImmersiveMode() {
if (Build.VERSION.SDK_INT >= 19) {
View.OnSystemUiVisibilityChangeListener listener = new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
enterImmersiveMode();
}
}
};
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(listener);
enterImmersiveMode();
}
}
@SuppressLint("NewApi")
private void enterImmersiveMode() {
if (Build.VERSION.SDK_INT >= 19) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}