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]