管理系统UI之五:响应UI可见性的变化(Responding to UI Visibility Changes)

leecosy 9年前

 本节课将教你如果注册监听器来监听系统UI可见性的变化。这个方法在将系统栏与你自己的UI控件进行同步操作时很有用。

 

注册监听器

为了获取系统UI可见性变化的通知,我们需要对View注册View.OnSystemUiVisibilityChangeListener监听器。通常上来说,这个View是用来控制导航的可见性的。

例如你可以添加如下代码在onCreate中

View decorView = getWindow().getDecorView();  decorView.setOnSystemUiVisibilityChangeListener          (new View.OnSystemUiVisibilityChangeListener() {      @Override      public void onSystemUiVisibilityChange(int visibility) {          // Note that system bars will only be "visible" if none of the          // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.          if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {              // TODO: The system bars are visible. Make any desired              // adjustments to your UI, such as showing the action bar or              // other navigational controls.          } else {              // TODO: The system bars are NOT visible. Make any desired              // adjustments to your UI, such as hiding the action bar or              // other navigational controls.          }      }  });

保持系统栏和UI同步是一种很好的方式,比如当状态栏显示或隐藏的时候进行Action Bar的显示和隐藏。