为什么 navigationBars() 返回全屏 activity?
Why does the navigationBars() come back on Fullscreen activity?
我 Android API 30 岁以上,我想制作一个全屏应用程序。
我在这里禁用了 navigationBars()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySplashscreenBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContentView = binding.fullscreenContent;
mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
但是,当我点击任何地方时,navigationBars() 返回。
如果我做第二个click/touch,它又消失了,每次都是这种情况。
有什么想法吗?
几个小时后,我找到了解决方案:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
fullSceen(getWindow().getDecorView());
}
else {
try {
Object wmgInstance = Class.forName("android.view.WindowManagerGlobal").getMethod("getInstance").invoke(null);
Field viewsField = Class.forName("android.view.WindowManagerGlobal").getDeclaredField("mViews");
viewsField.setAccessible(true);
ArrayList<View> views = (ArrayList<View>) viewsField.get(wmgInstance);
for (View view: views) {
fullSceen(view);
view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
fullSceen(view);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
AppUtils.console(this,TAG, "hasFocus == false");
}
}
private void fullSceen(View view){
view.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);
}
自 Android 30+ 以来贬值。对于最后 API,使用 :
binding = ActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContentView = binding.fullscreenContent;
mContentView.getWindowInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
感谢@Hong Nguyen
我 Android API 30 岁以上,我想制作一个全屏应用程序。
我在这里禁用了 navigationBars()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySplashscreenBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContentView = binding.fullscreenContent;
mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
但是,当我点击任何地方时,navigationBars() 返回。
如果我做第二个click/touch,它又消失了,每次都是这种情况。
有什么想法吗?
几个小时后,我找到了解决方案:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
fullSceen(getWindow().getDecorView());
}
else {
try {
Object wmgInstance = Class.forName("android.view.WindowManagerGlobal").getMethod("getInstance").invoke(null);
Field viewsField = Class.forName("android.view.WindowManagerGlobal").getDeclaredField("mViews");
viewsField.setAccessible(true);
ArrayList<View> views = (ArrayList<View>) viewsField.get(wmgInstance);
for (View view: views) {
fullSceen(view);
view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
fullSceen(view);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
AppUtils.console(this,TAG, "hasFocus == false");
}
}
private void fullSceen(View view){
view.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);
}
自 Android 30+ 以来贬值。对于最后 API,使用 :
binding = ActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContentView = binding.fullscreenContent;
mContentView.getWindowInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
感谢@Hong Nguyen