2011-11-25 81 views
21

我想創建一個按鈕,我可以在平板電腦上隱藏或顯示狀態欄。Android:顯示/隱藏狀態欄/電源欄

我已經把在OnCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 

,並在按鍵 顯示:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

隱藏:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

任何提示/的竅門?

//編輯

我看了這個提示的位置:http://android.serverbox.ch/?p=306 和改變了我這樣的代碼:

private void hideStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"}); 
    proc.waitFor(); 
} 

private void showStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"}); 
    proc.waitFor(); 
} 

所以,如果我點擊我的按鈕的方法被稱爲我可以看到有些事情正在發生,因爲該應用程序正在等待幾秒鐘。 我也看着LockCat,看到有什麼事情發生。

顯示:http://pastebin.com/CidTRSTi 隱藏:http://pastebin.com/iPS6Kgbp

+0

例http://stackoverflow.com/a/35886019/4395114 –

回答

78

你有全屏的主題在清單中設置?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 

我不認爲你沒有這個就能夠全屏。

我會用下面的添加和刪除全屏標誌:通過清除FLAG_FULLSCREEN可能無法正常工作

// Hide status bar 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
// Show status bar 
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
+0

不,我還沒有把它添加到清單。但是,當我嘗試時,我得到的錯誤:錯誤:找不到匹配給定名稱的資源('主題'值'@android:style/ Theme.Dark.NoTitleBar.Fullscreen')。「 – B770

+1

@ B770:有沒有一個名爲'Theme.Dark.NoTitleBar.Fullscreen',有一個名爲'Theme.NoTitleBar.Fullscreen',但是你不能擺脫缺少離屏HOME和BACK按鈕的Android 3.0+設備上的系統欄。 – CommonsWare

+0

@CommonWare:我發現這個: http://forum.xda-developers.com/showthread.php?t=1265397 這裏可以隱藏和顯示狀態欄我已經安裝了「克服」ROM的地方這個funktion是包含的,它的工作原理,所以我也可以把這個funktion放在另一個按鈕上... – B770

25

對於一些人來說,顯示狀態欄,

這裏是爲我工作的解決方案(Documentation)(Flag Reference

隱藏狀態欄

// Hide Status Bar 
if (Build.VERSION.SDK_INT < 16) { 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} 
else { 
    View decorView = getWindow().getDecorView(); 
    // Hide Status Bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
} 

顯示狀態欄

if (Build.VERSION.SDK_INT < 16) { 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 
    else { 
     View decorView = getWindow().getDecorView(); 
     // Show Status Bar. 
     int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
     decorView.setSystemUiVisibility(uiOptions); 
    } 
-2

參考 - https://developer.android.com/training/system-ui/immersive.html

// 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

一個在奇巧介紹的功能是 「沉浸式模式」。沉浸模式讓用戶能夠通過滑動來顯示/隱藏狀態欄和導航欄。要嘗試,請點擊「切換沉浸式模式」按鈕,然後嘗試刷入酒吧!

例子:

public void toggleHideyBar() { 

     int uiOptions = getActivity().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; 
     } 

     if (Build.VERSION.SDK_INT >= 18) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
     } 

     getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
     //END_INCLUDE (set_ui_flags) 
    }