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);


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();


三、Hiding the Navigation Bar


在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


發佈了26 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章