Managing the System UI

遇到4.4.2平板全屏问题,试了很多办法都失败了,后来在官网里面找到了,使用Immersive设置全屏,本人英文不好,所以只是把大概的方法做了下简单的记录

一、Diming the System Bars

1、Dim the Status and Navigation Bars在Android4.0 或者更高版本中使用SYSTEM_UI_FLAG_LOW_PROFIEL 代码如下:

// This example uses decor view, but you can use any visible view.

View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);

Managing the System UI_第1张图片

2、Reveal the Status and Navigation Bars

如何想清除由setSystemUiVisibility()设置的flags,可以使用如下方法

View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);

二、Hiding the Status Bar

1、在Android4.0或者低版本中隐藏Status Bar 

可以通过在Android4.0或者低版本中设置WindowManager 的flag来隐藏Status bar,设置应用中manifes中的app的主题属性,如下

<application
    ...
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    ...
</application>

优点:容易维护不易出错,产生更平滑的界面过度

或者如下

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
    }
    ...
}

2、在Android4.1或者更高版本中隐藏Status Bar

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

Managing the System UI_第2张图片

三、Hiding the Navigation Bar

Managing the System UI_第3张图片

在Android4.0或更高版本中隐藏Navigation Bar 

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);


注意:
1、使用此方法,当点击屏幕的任意位置,Navigation bar(和 status bat)会重新出现并且一直显示,用户先前设置的所有flags会被清除掉
2、一旦flags 被清除,如果你想你的应用中再次hide the bar 你就需要重置。参考Responding to UI Visibility Changes 关于如何监听UI 显示变更的讨论,便于你的应用可以给予响应
3、设置UI falg的位置不同效果也不同。如果在Activity 的 onCreate() 方法和点击home 键时隐藏 system bar,the system bar还会再次出现。当用户重启Activity,onCreate()方法将不会被调用,所以the system bar将会重现显示并一直存在,如果想system UI变更始终保持Navigation 隐藏无论是在Activity内外,在onResume()或者onWindowFocusChanged()方法中设置UI falgs


四、Using Immersive Full-Screen Mode

1、Use Non-Sticky Immersion确保action bar和其他UI controls 同时隐藏。以下代码片段示范如何显示/隐藏Status和navigation bars,在没有调整内容尺寸的时候。

// This snippet hides the system bars.
private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    mDecorView.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 // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}


2、Use Sticky Immersion

当使用IMMERSIVE_STICKY flag 时 system bars短暂出现然后隐藏,以下代码片段使用了这个flag,任何时候当窗口获得焦点,简单的设置IMMERSIVE_STICKY flag,同其他的flag一起配置:

@Override
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);}
}



https://developer.android.com/training/system-ui/immersive.html#nonsticky


你可能感兴趣的:(android,全屏,SystemUI,隐藏系统栏)