2010-10-04 126 views
2

我正在尋找一種方式來顯示和隱藏狀態欄與onClickListener,但只顯示它的作品。如何切換狀態欄?

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
if (isStatusbarVisible) 
    lp.flags = LayoutParams.FLAG_FULLSCREEN; 
else 
    lp.flags = LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; 
getWindow().setAttributes(lp); 
isStatusbarVisible = !isStatusbarVisible; 

隱藏使用FLAG_FULLSCREEN狀態欄,似乎只有在標誌調用setContentView()之前設置工作。

是否有另一種隱藏狀態欄的方法?

回答

4

答案很簡單,清除FLAG_FULLSCREEN標誌是所有必要的多數民衆贊成:

if (isStatusBarVisible) 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
else 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
1

我一直在尋找這種解決方案,終於想出了這個實施切換全屏和關閉:

private void toggleFullscreen() { 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    getWindow().setAttributes(attrs); 
} 

它使用按位異或邏輯切換FLAG_FULLSCREEN

0

有一個更好的(不需要布爾變量)切換全屏方法實現:

private void toggleFullscreen() { 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    getWindow().setAttributes(attrs); 
} 

它採用按位異或邏輯切換FLAG_FULLSCREEN