1. 去除頂部標題欄TitleBar
Android在真機調試時,如果不做特殊設置,每個活動界面的頂部會留有標題欄,如下圖所示:
自然標題欄的存在影響了界面整體的美觀,解決這一問題的方法主要有以下兩種,這裏推薦使用第一種方法,第二種方法在剛進入界面時可能會經過一段時間界面才能加載出來。
- 方法一:設置活動界面的主題Theme
在AndroidManifest清單文件中,可以對每個Activity設定主題樣式,例如對於下面的Activity
<activity
android:theme="@style/splashTheme"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
設定的主題樣式爲style樣式下的splashTheme(這個主題是自己定義的,用於歡迎界面)。我們到res→values→style.xml文件中可以看到主題splashTheme的定義如下:
<style name="splashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/splash</item>
</style>
(剛纔介紹了AndroidManifest清單文件中對Activity主題的設定方式,下面回到我們剛纔的問題)
其實我們沒有必要一個個的對每個Activity設定主題樣式(對某個Activity特殊處理的情況除外),可以在Application標籤中統一設定App所有活動界面的主題,如下:
<application
android:theme="@style/AppTheme"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:theme="@style/splashTheme"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
要設定Activity無標題欄,只需要在主題樣式(這裏是AppTheme)中添加標籤 <item name="android:windowNoTitle">true</item> 即可實現,此時AppTheme內容如下:
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item>
</style>
這時候再運行我們的App,就可以看到想要的效果啦。
- 方法二:界面加載前請求取消標題欄 (Java代碼方式)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄
setContentView(R.layout.activity_main);
}
2. 沉浸式狀態欄 (將ActivityLayout背景延伸到狀態欄)
實際上僅僅去除標題欄還不是我們最終的目標,我們的目標是將Activity的背景延伸到狀態欄,實現沉浸式狀態欄,先放效果圖。
是不是想要的效果?別急,很簡單,慢慢來。整體過程分爲以下兩步:
Step1. 設定ActivityLayout背景
背景可以是動畫Animal,也可以是簡單的背景圖片或顏色填充,直接在佈局xml文件根佈局中設定background屬性即可,這裏以圖片背景爲例。如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
</RelativeLayout>
Step2. 設定主題Theme樣式
上面已經介紹瞭如何設定清單文件更改Activity的主題,類似地也可以通過設定主題樣式實現沉浸式狀態欄,具體的style如下:
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
</style>
下面對上述主題樣式做個簡單的說明:
<item name="android:windowNoTitle">true</item> 上面已經介紹了,它的作用是去除Activity的標題欄;
<item name="android:windowContentOverlay">@null</item> 的作用是防止Activity出現白屏,因此設定爲無遮蓋;
<item name="android:windowTranslucentStatus">true</item> 的作用即爲設定狀態欄爲透明,也就是這裏講到的背景延伸;
<item name="android:fitsSystemWindows">true</item> 的作用是防止控件跑到狀態欄上,因此設定爲適應屏幕窗口大小。
以上是個人在初學Android原生開發時的一些總結,希望對您有所幫助。
聯繫郵箱:[email protected]