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

来源 http://hukai.me/android-training-course-in-chinese/ui/system-ui/visibility.html

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


注册监听器

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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的显示和隐藏。

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