Android IMMERSIVE模式无法正常工作

时间:2015-05-11 06:40:22

标签: android

我制作Android 5.x APP

我想要IMMERSIVE mode = fullscreen

因此...

MainActivity setOnSystemUiVisibilityChangeListener成功致电

Sub Activity移动setOnSystemUiVisibilityChangeListener来电无效

帮帮我......

=================

MainActivity.java

的onCreate

UiChangeListener();

public  void UiChangeListener()
{   
    decorView = getWindow().getDecorView(); 

       decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
       @Override
       public void onSystemUiVisibilityChange(int visibility) {   
           if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {  
               decorView.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);
           }
       }
   });

}

SubActivity.java

的onCreate

UiChangeListener();

public  void UiChangeListener()
{   
    decorView = getWindow().getDecorView(); 

       decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
       @Override
       public void onSystemUiVisibilityChange(int visibility) {   
           if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {  
               decorView.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);
           }
       }
   });

}

子活动 uichangeListener()无法正常工作

1 个答案:

答案 0 :(得分:1)

如文档https://developer.android.com/training/system-ui/immersive.html#sticky中所述,您应该像这样调用沉浸式初始化:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        decorView.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);
    }
}

在SubActivity中覆盖onWindowFocusChanged。