和尚我最近在調整頁面狀態欄的效果,主要包括沉浸式狀態欄和僞沉浸狀態欄(同事嘮嗑給定義的玩的)。
前段時間整理過一篇 Android 沉浸式狀態欄的多種樣式,如今和尚我在稍微的補充一下,都是在平常應用中測試整理的。android
非 Toolbar 標題欄
就和尚我接觸的項目中根據業務不一樣,不是全部的標題欄都是 Toolbar 標題欄樣式,不少是自定義的標題欄樣式,爲了效果統一,和尚個人解決方案是修改頂部狀態欄的顏色爲程序的主題色,戲稱爲僞沉浸式狀態欄。
如下是和尚我本身測試的最簡單的標題欄樣式:微信
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:background="@color/colorAccent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:paddingLeft="6dp"
android:src="@mipmap/icon_back_white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我是標題"
android:textColor="@android:color/white"
android:textSize="18sp" />
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我是內容"
android:textSize="18sp" />
</LinearLayout>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_toolbar)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val window = window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = resources.getColor(R.color.colorAccent)
//window.statusBarColor = Color.TRANSPARENT
//window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
}
圖1網絡
圖2app
Tips1: Window 是一個很值得研究的類,設置 statusBarColor 屬性便可修改狀態欄顏色。
Tips2: 若配合打開代碼中註釋的兩行,總體的效果是隱藏掉狀態欄高度,標題欄上移,如圖2所示,在其餘相應的場景下頗有用。ide
Toolbar 標題欄
和尚我花了很多時間在之前的博客中,你們能夠移步審查一下。如今和尚又用了一種方式,主要是爲了知足實時網絡更換主題圖,採用 背景主題色+透明圖層 方式。若是不須要來回更換圖片能夠直接用 layer-list 的 drawable 方式,如今須要隨意更換圖片,因此和尚我把這主題色和透明塗層區分開。
如下是和尚加載一張圖片的 Toolbar 方式:佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.roating.ace.ace06.ToolbarTestActivityK">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/icon_bg"
android:fitsSystemWindows="true"
android:gravity="center" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
override fun onCreate(savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val window = window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_toolbar_test)
}
和尚我是在 Toolbar 外添加一層 LinearLayout 做爲背景主題色塗層,Toolbar 添加背景圖,而 Toolbar 的位置寬高等都可按需求定義,並配合上面剛提到的 Tips2 方式處理如下。測試
Tips1: 網上有人說 window 的設置須要放在 setContentView 加載佈局文件以前,和尚我特地用先後兩種位置嘗試,效果是一致的。
Tips2: 在使用 window.statusBarColor 時,會提示:Call requires API level 21(current min is 15):android.view.Window#setStatusBarColor,此時不建議用 @TargetApi(Build.VERSION_CODES.KITKAT) 這種方式,這樣會固定一個版本,且頂部狀態欄有時會修改無效,建議用如上 if方式 判斷處理。ui
下面是和尚個人公衆號,歡迎閒來吐槽~spa
本文分享自微信公衆號 - 阿策小和尚(gh_8297e718c166)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。.net