Android 沈浸式狀態欄
效果圖
- android 5.0 以上
- android 4.4 API 19
以上都是原生安卓系統的效果,具體到國內的各種各樣改過的系統可能會有細微差別,測試過小米和華為的機器效果基本一樣。
實現
1.修改主題屬性
方法一:
在values-v19文件夾下聲明AppTheme為透明狀態欄,代碼如下
1 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 2 <!-- Customize your theme here. --> 3 <item name="android:windowTranslucentStatus">true</item> 4 </style>
方法二:但是實際測試中發現在國產某些rom上,xml聲明的會不起作用,在代碼裏直接聲明更有效。在onCreate方法下聲明。
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState);
5 super.setContentView(R.layout.activity_base);6 // 經測試在代碼裏直接聲明透明狀態欄更有效 7 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 8 WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes(); 9 localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);10 } 11 }
2.設置fitsSystemWindows屬性
如果你想讓一個View的圖像顯示在狀態欄下,那麽就在View的XML布局文件中添加如下屬性
1 android:fitsSystemWindows="true"
例子1:
這裏我設置了在:AppBarLayout
註意:如果你在同一個布局中添加了多個這個屬性,那麽一般只有最外層View的這個 android:fitsSystemWindows="true” 屬性生效
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.design.widget.CoordinatorLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 app:layout_behavior="@string/appbar_scrolling_view_behavior"> 9 10 <android.support.design.widget.AppBarLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:fitsSystemWindows="true"> 14 15 <android.support.v7.widget.Toolbar 16 android:id="@+id/toolbar" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:background="@color/colorPrimary" 20 android:minHeight="?attr/actionBarSize" 21 android:paddingTop="25dp" 22 app:layout_scrollFlags="scroll|enterAlways" 23 app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 24 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 25 26 </android.support.design.widget.AppBarLayout> 27 </android.support.design.widget.CoordinatorLayout>
例子2:
在Activity的布局文件中把android:fitsSystemWindows設置為true
雖然不加android:fitsSystemWindows屬性也會改變狀態欄的背景,但是出現的結果是不同的,不明白的話可以看例2的圖比較直觀。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:fitsSystemWindows="true" 7 android:orientation="vertical" 8 tools:context="wxt.example.com.as20.MainActivity" 9 android:background="#FF4081"> 10 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="Hello World!" /> 15 <Button 16 android:onClick="open" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="打開"/> 20 21 </LinearLayout>
這個是個LinearLayout布局,我們把android:fitsSystemWindows屬性在裏面設置。如果background是一種顏色的話,他會在整個屏幕上都是這種顏色。 那麽我們把android:fitsSystemWindows屬性去掉之後呢,兩者有什麽差別呢?
加了android:fitsSystemWindows屬性的界面(左) 不加android:fitsSystemWindows屬性的界面(右)
3.調整View高度
上面兩步都是統一的,這一步就比較有針對性了,對不同布局和API版本都會有所微調,主要是頂部View的高度。
如果你像我一樣基本使用原生控件,那麽一般情況下是調整ToolBar(ActionBar)的高度。你需要給Toolbar加上系統狀態欄的高度,因為如果你設置了前面兩步,那麽ToolBar會上移到狀態欄下面,如圖
方法一:我比較喜歡的處理方式是在XML中定義高度,那麽就需要寫一些分離區分版本的XML文件。目前的話安卓手機端除6.0的系統狀態欄是24dp,其它都是25dp。
1 android:paddingTop="25dp"
方法二:是在java代碼中改變高度,註意需要判斷安卓版本,樣例如下:
(具體獲取狀態欄高度的代碼可以到後面的參考資料中看,也可以在Demo中看源碼)
1 mToolbar = (Toolbar) findViewById(R.id.toolbar); 2 3 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 4 { 5 mToolbar.getLayoutParams().height = getAppBarHeight(); 6 mToolbar.setPadding(mToolbar.getPaddingLeft(), 7 getStatusBarHeight(), 8 mToolbar.getPaddingRight(), 9 mToolbar.getPaddingBottom()); 10 } 11 12 setSupportActionBar(mToolbar);
參考資料
Android 沈浸式狀態欄完美實現
薄荷TOOLBAR(ACTIONBAR)的適配方案
Android中 4.4-5.0 系統狀態欄顏色的修改。實現Translucent System Bar
Demo下載
【GitHub】
Android 沈浸式狀態欄