轉載說明:原貼地址:http://blog.csdn.net/a_running_wolf/article/details/50480386 html
設置Activity隱藏標題欄、設置Activity全屏顯示在咱們開發中會常常用到,畢竟手機屏幕大小有限,有時候是爲了顯示更多的信息而捨棄一些沒必要要的界面開支,首當其衝的就是標題欄,接下來就是狀態欄;有時候是爲了界面的簡潔和美觀……不舉例子了,相信你確定遇到過這樣的需求,直接入正題:java
設置隱藏標題欄、全屏顯示經常使用的有2種方法(和Android開發中大多數屬性的設置方法同樣):android
在AndroidManifest.xml文件的相應Activity節點下設置其android:theme屬性值,該屬性值(使用系統theme值,自定義theme除外)通常以"@android:style/Theme."開頭,如android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"。下邊就來講說這些theme有哪些不一樣及各自的效果(以Android4.4.2爲例):字體
咱們在界面上添加一個TextView輔助觀察:this
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="全屏設置及Theme屬性解析" android:textSize="20sp"/> <!-- 這裏只設置了字體大小,並未設置字體顏色 -->
*注意*這裏只設置了字體大小並未設置字體顏色,下邊會涉及到。spa
<activity android:name="com.wangj.fullscreen.TestActivity" android:label="@string/title_activity_test" android:theme="@android:style/下邊說到的theme值" android:icon="@drawable/ic_launcher" android:screenOrientation="portrait" > <!-- 添加android:theme屬性 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Theme.Black黑色主題:標題欄灰色、不顯示icon(即便設置了也不顯示)、有狀態欄、TextView字體反色:.net
Theme.Black.NoTitleBar:黑色主題、無標題欄、有狀態欄、字體反色:code
Theme.Black.NoTitleBar.Fullscreen:黑色主題、無標題欄、無狀態欄、字體反色:xml
Theme.Light白色主題:標題欄灰色、不顯示icon(即便設置了也不顯示)、有狀態欄、TextView字體黑色:htm
Theme.Light.NoTitleBar:白色主題、無標題欄、有狀態欄、TextView字體黑色
Theme.Light.NoTitleBar.Fullscreen:白色主題、無標題欄、無狀態欄、Textiew黑字
從上邊能夠看出:"Theme"是一個域,"NoTitleBar"指無標題欄,"FullScreen"指無狀態欄。
Theme.Wallpaper:與黑色主題相似,只是北京變爲手機牆紙圖片
一樣,Theme.Wallpaper.NoTitleBar:同上,無標題欄
Theme.Wallpaper.NoTitleBar.Fullscreen:同上,無標題欄、無狀態欄
Theme.Translucent:半透明(說是半透明,但看效果是透明的)
一樣的,Theme.Translucent.NoTitleBar:同上,去掉了標題欄
Theme.Translucent.NoTitleBar.Fullscreen:同上,去掉了標題欄和狀態欄
Theme.Holo( | .NoActionBar(難道在Holo中TitleBar更名了) | .NoActionBar .Fullscreen):質樸風,黑色背景藍色標題欄底、標題欄可顯示icon、TextView反色字( | 去掉標題欄 | 去掉標題欄和狀態欄)
Theme.Holo.Light( | .NoTitleBar | .NoTitleBar.Fullscreen):質樸風,銀色背景、標題欄可顯示icon、TextView黑字( | 去掉標題欄 | 去掉標題欄和狀態欄)
Theme.Holo.Wallpaper( | .NoTitleBar | 沒有.NoTitleBar .Fullscreen):牆紙背景質樸風,標題欄有藍色底邊( | 去掉標題欄 | 狀態欄去不掉)
*注意*
沒有Theme.Holo.Black,Theme.Holo默認就是黑色背景的
系統沒有Theme.Holo.Translucent
其實,看完上邊你們只要記住Black、Light、Holo、Translucent、Wallpaper、NoTitleBar、NoActionBar、FullScreen……等關鍵字和其意義(更多關鍵字的功能有待探索),用的時候在系統列表裏找相應組合就能夠了,好比上邊要隱藏標題欄的、要設置全屏的根據須要找相應的Theme就能夠了。至於沒有的也不能本身創造,只能自定義了。
在Activity的onCreate()方法中,在setContent()以前採用如下語句設置
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 隱藏應用程序的標題欄,即當前activity的label this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隱藏android系統的狀態欄
*注意*必須在setContent()方法以前設置,不然沒有效果,由於setContent方法設置"View繪製區域"所顯示的組件,而狀態欄、標題欄都是View繪製區以外的區域(應用區、屏幕)