android 沉浸栏灰色,Android 沉浸栏实践——踩坑
當前開發環境:Android Studio 2.1.3,compileSdkVersion 24,buildToolsVersion "24.0.2",support:appcompat-v7:24.2.0
首先放個圖,這就是我要做成的效果,Toolbar 和 Status Bar 一體共用背景圖,實際上就是 Toolbar 的背景圖延伸到 Status Bar。
效果圖
先做一點點思考。我不打算修改 toolbar 的高度,設置為 android:layout_height="?attr/actionBarSize" 就好,否則 fitsSystemWindows 之后就需要設置 toolbar 高度為 25dp + 48dp = 73dp,同時其他的內容也會改變。那么,可以考慮在 AppBarLayout 中設置背景,然后讓它侵入到狀態欄去。現在 style.xml 看起來會是這樣:
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
false
true
true
false
true
@android:color/transparent
false
true
toolbar 的布局文件差不多是這樣子:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:paddingTop="@dimen/appbar_top_padding"
android:background="@drawable/bg_bar"
android:theme="@style/AppTheme.AppBarOverlay">
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
主布局文件差不多是這樣子:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.iamwent.tinter.MainActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
android:id="@+id/tv_sdk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"/>
然后你運行,發現 v21 的效果出來了,但是 v19 是這么個鬼!
v19效果
那個灰色條是什么?又是怎么來的呢?給跟布局設置背景色就可以發現,灰色條實際上是因為根布局侵入了狀態欄,從我們給根布局設置的 android:fitsSystemWindows="true" 就是讓布局上去
對于 android:windowTranslucentStatus文檔上是這么說的:
By enabling translucent system bars, your layout will fill the area behind the system bars, so you must also enable fitsSystemWindows
for the portion of your layout that should not be covered by the system bars.
開啟透明狀態欄后,你的布局會填充狀態欄下面的區域,所以你應當同時設置布局 fitsSystemWindows 以防止被狀態欄覆蓋。
而對于 android:fitsSystemWindows 文檔又是這么說的:
Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.
配置 fitsSystemWindows 后,系統就會調整 view 的 padding 以給 system windows 留出空間。
解決辦法是根布局不設置 android:fitsSystemWindows,然后在代碼中判斷,在 v21 以上手動設置這一屬性,代碼如下:
private void setTranslucentStatusBar() {
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
setTranslucentStatusBarLollipop();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setTranslucentStatusBarLollipop() {
((ViewGroup) getWindow().findViewById(android.R.id.content)).getChildAt(0).setFitsSystemWindows(true);
}
昨晚吐槽的時候,有位朋友提醒了我,這個值也可以用 style 的方式設置:
true
我腦袋一抽,覺得以下這個方式可能也可以:
false
true
坑位零
在上面的配置中涉及到了多個屬性的設置,建議修改為不同的值,看看它們會造成什么樣的效果。
android:statusBarColor
android:windowDrawsSystemBarBackgrounds
坑位一
奇怪的間隔
眼尖的人可能在最前面的效果圖就看到了,那就是在 v23 上這個顯示系統版本的 TextView 有一個奇怪的 margin,而且很巧合的就是 25dp!最后我發現,是由于給主布局的 RelativeLayout 設置了 app:layout_behavior="@string/appbar_scrolling_view_behavior" 造成的。解決辦法是在 v23 上給 RelativeLayout 設置 android:layout_marginTop="-25dp",可以在 dimens 中做。
坑位二
在修改的過程中我還發現一個奇怪的問題,status bar 是個灰色的條,超級奇怪!最后對比發現是 AppTheme.NoActionBar 采用繼承 Theme.AppCompat.Light.NoActionBar 的方式,解決辦法是不繼承 parent,采用手動配置。
Theme 造成的怪現象
結語
我是要給一個半成品的 APP 做適配,所以先單獨做了一個 demo 實現想要的——后來踩到的坑證明我這個決定是多么的正確!特別是坑位三。所以一旦出現莫名其妙的錯誤,我就對照 demo 的配置一個個地方去排除。
另外,多試試各種配置的作用,明白它們影響的是什么區域,碰到問題才好修改。
總結
以上是生活随笔為你收集整理的android 沉浸栏灰色,Android 沉浸栏实践——踩坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想要以卡办卡申请信用卡 你需要了解这些事
- 下一篇: 电脑用linux命令大全,电脑操作时常用