Hiding the Status Bar(Android設(shè)置全屏)

This lesson describes how to hide the status bar on different versions of Android. Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more immersive user experience.

Figure 1 shows an app with a visible status bar

Visible status bar

Figure 2 shows an app with a hidden status bar. Note that the action bar is hidden too. You should never show the action bar without the status bar

Hidden status bar

Hide the Status Bar on Android 4.0 and Lower

You can hide the status bar on Android 4.0 (API level 14) and lower by setting?WindowManager?flags. You can do this programmatically or by setting an activity theme in your app's manifest file. Setting an activity theme in your app's manifest file is the preferred approach if the status bar should always remain hidden in your app (though strictly speaking, you could programmatically override the theme if you wanted to). For example:

The advantages of using an activity theme are as follows:

1麻汰、It's easier to maintain and less error-prone than setting a flag programmatically.

2追葡、It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.

Alternatively, you can programmatically set?WindowManager?flags. This approach makes it easier to hide and show the status bar as the user interacts with your app:

When you set?WindowManager?flags (whether through an activity theme or programmatically), the flags remain in effect unless your app clears them.

You can use?FLAG_LAYOUT_IN_SCREEN?to set your activity layout to use the same screen area that's available when you've enabled?FLAG_FULLSCREEN. This prevents your content from resizing when the status bar hides and shows.

Hide the Status Bar on Android 4.1 and Higher

You can hide the status bar on Android 4.1 (API level 16) and higher by using?setSystemUiVisibility().?setSystemUiVisibility()?sets UI flags at the individual view level; these settings are aggregated to the window level. Using?setSystemUiVisibility()?to set UI flags gives you more granular control over the system bars than using?WindowManager?flags. This snippet hides the status bar:

Note the following:

1、Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. SeeResponding to UI Visibility Changesfor a discussion of how to listen for UI visibility changes so that your app can respond accordingly.

2初澎、Where you set the UI flags makes a difference. If you hide the system bars in your activity'sonCreate()method and the user presses Home, the system bars will reappear. When the user reopens the activity,onCreate()won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags inonResume()oronWindowFocusChanged().

3嚎货、The methodsetSystemUiVisibility()only has an effect if the view you call it from is visible.

4橘霎、Navigating away from the view causes flags set withsetSystemUiVisibility()to be cleared.

Make Content Appear Behind the Status Bar

On Android 4.1 and higher, you can set your application's content to appear behind the status bar, so that the content doesn't resize as the status bar hides and shows. To do this, use?SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You may also need to use?SYSTEM_UI_FLAG_LAYOUT_STABLE?to help your app maintain a stable layout.

When you use this approach, it becomes your responsibility to ensure that critical parts of your app's UI (for example, the built-in controls in a Maps application) don't end up getting covered by system bars. This could make your app unusable. In most cases you can handle this by adding theandroid:fitsSystemWindowsattribute to your XML layout file, set totrue. This adjusts the padding of the parent?ViewGroup?to leave space for the system windows. This is sufficient for most applications.

In some cases, however, you may need to modify the default padding to get the desired layout for your app. To directly manipulate how your content lays out relative to the system bars (which occupy a space known as the window's "content insets"), override?fitSystemWindows(Rect insets). The?fitSystemWindows()?method is called by the view hierarchy when the content insets for a window have changed, to allow the window to adjust its content accordingly. By overriding this method you can handle the insets (and hence your app's layout) however you want.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市殖属,隨后出現(xiàn)的幾起案子姐叁,更是在濱河造成了極大的恐慌,老刑警劉巖洗显,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件外潜,死亡現(xiàn)場離奇詭異,居然都是意外死亡挠唆,警方通過查閱死者的電腦和手機(jī)处窥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來玄组,“玉大人滔驾,你說我怎么就攤上這事《矶铮” “怎么了哆致?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長患膛。 經(jīng)常有香客問我摊阀,道長,這世上最難降的妖魔是什么踪蹬? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任胞此,我火速辦了婚禮,結(jié)果婚禮上延曙,老公的妹妹穿的比我還像新娘豌鹤。我一直安慰自己,他們只是感情好枝缔,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布布疙。 她就那樣靜靜地躺著蚊惯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪灵临。 梳的紋絲不亂的頭發(fā)上截型,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天,我揣著相機(jī)與錄音儒溉,去河邊找鬼宦焦。 笑死,一個胖子當(dāng)著我的面吹牛顿涣,可吹牛的內(nèi)容都是我干的波闹。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼涛碑,長吁一口氣:“原來是場噩夢啊……” “哼精堕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蒲障,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤歹篓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后揉阎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庄撮,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年毙籽,在試婚紗的時候發(fā)現(xiàn)自己被綠了洞斯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡惧财,死狀恐怖巡扇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情垮衷,我是刑警寧澤厅翔,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站搀突,受9級特大地震影響刀闷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜仰迁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一甸昏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧徐许,春花似錦施蜜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缸沃。三九已至,卻和暖如春修械,著一層夾襖步出監(jiān)牢的瞬間趾牧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工肯污, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留翘单,地道東北人。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓蹦渣,卻偏偏與公主長得像哄芜,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子柬唯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容