【轉】Android 全屏方案(隱藏NavigationBar)


http://www.07net01.com/2015/04/822292.html

android4.0及其以上的版本中,出現了一個很屌的東西,叫做Navigation Bar,它和Status Bar

一上一下相互交映,影響了我們的全屏。如果還不知道Navigation Bar是個毛,請看下圖:

navigation-bar

1. 低級隱藏

這個東西其實是可以隱藏和顯示的,下面是Google給出的方法,此方法必須在Android 4.0以上

的系統中使用,當然4.0以下也沒有(也就是api14),直接把這段代碼粘貼在,你的Activity的

onCreate()方法中就可以了:

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

但是這么做是有缺陷的,Google共給出了5個注意事項,我只看懂以下介個:

a) 使用這種設置flag的方式雖然暫時隱藏了Navigation Bar,但是用戶觸摸屏幕的任何地方

flags將會被清除,也就是說你的設置,在用戶觸摸屏幕后會失效

b) 一但你設置的flags被清除后,如果你再想隱藏Navigation Bar,需要重新設置,這個需要

監聽一個事件,看文章最后:

c) 你設置UI flags的位置很重要,也就是上面的代碼的放置位置,如果你放到onCreate()方法

中的時候,如果用戶通過Home鍵回到桌面,當你重新打開的時候,由於這是onCreate()

方法不會再次被調用,所以Navigation Bar 不再隱藏。如果你想要你的設置在上面的情況

有效你需要在 onResume() or onWindowFocusChanged()

中設置UI flags

d)The method setSystemUiVisibility() only has an effect if the view you call it

from is visible.(哥實在是翻譯不出來,也沒看懂)我的翻譯:只有你調用setSystemUiVisibility()

這個方法的View是可見狀態下的時候,這個方法才會起作用。我們都知道,其實Activity的顯示是有一個View的

對,就是那個從window中調用的getDecorView()方法,每個Activity都有一個decorview對象,這個就是Activity

的渲染機制了。

e) Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

(額,這句英語也是屌的不行)同樣我的翻譯:

當導航離開這個View的時候會使得通過 setSystemUiVisibility()

這個方法設置的flags被清除,也就是說所有的在這個view上設置的全屏和隱藏NavigationBar的flag都將失效。

高級解決方案,通過按鈕事件控制Navigation Bar的顯示和隱藏

看下面的代碼來自Google的Example,只要在按鈕中調用這個方法,就可以隨意切換

顯示和隱藏Navigation Bar了

/**
     * Detects and toggles immersive mode (also known as "hidey bar" mode).
     */
    public void toggleHideyBar() {
 
        // The UI options currently enabled are represented by a bitfield.
        // getSystemUiVisibility() gives us that bitfield.
        int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        boolean isImmersiveModeEnabled =
                ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
        if (isImmersiveModeEnabled) {
            Log.i(TAG, "Turning immersive mode mode off. ");
        } else {
            Log.i(TAG, "Turning immersive mode mode on.");
        }
 
        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.version.SDK_INT >= 14) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
 
        // Status bar hiding: Backwards compatible to Jellybean
        if (Build.VERSION.SDK_INT >= 16) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }
 
        // Immersive mode: Backward compatible to KitKat.
        // Note that this flag doesn't do anything by itself, it only augments the behavior
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
        // all three flags are being toggled together.
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
        // Sticky immersive mode differs in that it makes the navigation and status bars
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
        // the screen.
        if (Build.VERSION.SDK_INT >= 18) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }
 
        getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
    }

Navigation Bar 透明遮擋內容

Android 越出越牛X,當Android4.1以后的版本,如果不是必須,不一定非要把他隱藏掉

因為這時候那個你的內容View可以放到Navigation Bar 后面,索引這個時候當navigation bar

顯示或者隱藏的時候,不會引起你的內容View重新計算大小。你可以使用SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION.

和 SYSTEM_UI_FLAG_LAYOUT_STABLE來實現。(個人目測這個用途不是太大吧)

Android 4.4Immersive Mode“沉浸式全屏”

這個比較好玩,Status Bar 和 Navigation Bar 是以透明的方式顯示的,但是這個要在api 19 之上使用。

沉浸式全屏是什么意思?就是支持沉浸式全屏的應用在Android 4.4的手機上會自動全屏顯示,並不會出現惱人的虛擬鍵

而當我們需要虛擬鍵的時候,只要在屏幕底部輕輕滑動一下即可調出虛擬鍵,而且虛擬鍵是以透明的狀態顯示的。

Android 4.4 中提供了 IMMERSIVE 和 IMMERSIVE_STICKY 標記, 可以用這兩個標記與

SYSTEM_UI_FLAG_HIDE_NAVIGATION 和 SYSTEM_UI_FLAG_FULLSCREEN 一起使用, 來實現沉 浸模式。

一個對沉浸模式的理解點擊這里

監聽事件的方式獲取屏幕Navigation Bar的變化,然后做出不同的處理

private static Handler sHandler;  
  
protected void onCreate(Bundle savedInstanceState){    
    super.onCreate(savedInstanceState);     
  
    sHandler = new Handler();  
      
    sHandler.post(mHideRunnable); // hide the navigation bar  
  
    final View decorView = getWindow().getDecorView();  
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()  
        {  
            @Override  
            public void onSystemUiVisibilityChange(int visibility)  
            {  
                sHandler.post(mHideRunnable); // hide the navigation bar  
            }  
        });          
}  
  
Runnable mHideRunnable = new Runnable() {  
    @Override  
    public void run() {  
        int flags;    
        int curApiVersion = android.os.Build.VERSION.SDK_INT;  
        // This work only for android 4.4+  
        if(curApiVersion >= Build.VERSION_CODES.KITKAT){  
            // This work only for android 4.4+  
            // hide navigation bar permanently in android activity  
            // touch the screen, the navigation bar will not show  
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  
                  | View.SYSTEM_UI_FLAG_IMMERSIVE  
                  | View.SYSTEM_UI_FLAG_FULLSCREEN;  
                  
        }else{  
            // touch the screen, the navigation bar will show  
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;  
        }  
          
        // must be executed in main thread 

:)

  
        getWindow().getDecorView().setSystemUiVisibility(flags);  
    }  
};    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM