透明狀態(tài)欄的多種方案悦即,可參考:棒棒糖:將其顏色設置為透明后面的狀態(tài)欄
本文只討論兩種方式
1厂置。主題中配置 <item name="android:windowTranslucentStatus">true</item>
可用于API 19及更高版本
2。主題中配置 <item name="android:statusBarColor">@android:color/transparent</item>
僅適用于API 21及更高版本
1鸦做,windowTranslucentStatus <item name="android:windowTranslucentStatus">true</item>
效果:狀態(tài)欄透明折欠,布局會自動填充statusbar(布局的內(nèi)容延伸到狀態(tài)欄),
android:fitsSystemWindows="true|false"
該屬性設置有效吼过,作用:true為布局添加高度為statusbar的paddingtop值锐秦,false不適配statusbar(默認false)
getWindow().getStatusBarColor() =-13615201 并不是Color.TRANSPARENT但是透明效果
缺點:彈出的dialog不會隨著輸入法滾動,可能導致輸入法遮擋住dialog
2盗忱,statusBarColor <item name="android:statusBarColor">@android:color/transparent</item>
效果:狀態(tài)欄透明酱床,布局不會自動填充statusbar(布局的內(nèi)容沒有延伸到狀態(tài)欄),statusbar背景為白色content的顏色
android:fitsSystemWindows="true|false"
該屬性設置無效
getWindow().getStatusBarColor() = Color.TRANSPARENT
不存在 方式一的缺點 dialog可以隨著輸入法滾動
要實現(xiàn)windowTranslucentStatus 的透明狀態(tài)欄的效果同時讓布局填充statusbar趟佃,android:fitsSystemWindows="true|false"
該屬性設置生效扇谣,需要設置window布局填充整個屏幕
設置window布局填充整個屏幕
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
透明狀態(tài)欄的實現(xiàn)方式 推薦使用statusBarColor
-
colorPrimaryDark
To set a custom color for the status bar,
use the android:statusBarColor attribute when you extend the material theme.
By default, android:statusBarColor inherits the value of android:colorPrimaryDark.
statusBar - 在
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
模式下,默認跟布局會延伸到statusBar狀態(tài)欄下面android:fitsSystemWindows="true"
是給View設置padingTop的值
在非SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
模式下,跟布局不延伸到statusBar狀態(tài)欄下面android:fitsSystemWindows="true"
的作用是允許View延伸到statusBar狀態(tài)欄下面,與填充屏幕的模式有差異
在沒有設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
布局中配置android:fitsSystemWindows="true"不會充滿屏幕,,
但是使用CoordinatorLayout包裹AppBarLayout
同時設置 android:fitsSystemWindows="true"那么布局可以填充整個屏幕
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
//充滿屏幕
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>