Android沉浸式效果的實現,狀態欄和導航欄均支持設置顏色、漸變色、圖片、透明度、內容入侵和狀態欄深色字體;兼容豎屏、橫屏,當屏幕旋轉時會自動適配。
現在我們的項目中經常需要適配StatusBar
和NavigationBar
,我們需要寫很多的代碼來進行適配,已完成所需功能。現在來介紹一個第三方Sofia來進行適配,大大的簡化了我們代碼的編寫。下面來看具體使用。
Sofia的GitHub地址:https://github.com/yanzhenjie/Sofia
首先,Sofia的引入,添加依賴
implementation 'com.yanzhenjie:sofia:1.0.5'
其次,提供的方法列表如下:
// 狀態欄深色字體。
Bar statusBarDarkFont();
// 狀態欄淺色字體。
Bar statusBarLightFont();
// 狀態欄背景色。
Bar statusBarBackground(int statusBarColor);
// 狀態欄背景 Drawable。
Bar statusBarBackground(Drawable drawable);
// 狀態欄背景透明度。
Bar statusBarBackgroundAlpha(int alpha);
// 導航欄背景色。
Bar navigationBarBackground(int navigationBarColor);
// 導航欄背景 Drawable。
Bar navigationBarBackground(Drawable drawable);
// 導航欄背景透明度。
Bar navigationBarBackgroundAlpha(int alpha);
// 內容入侵狀態欄。
Bar invasionStatusBar();
// 內容入侵導航欄。
Bar invasionNavigationBar();
// 讓某一個 View 考慮狀態欄的高度,顯示在適當的位置,接受 ViewId。
Bar fitsStatusBarView(int viewId);
// 讓某一個 View 考慮狀態欄的高度,顯示在適當的位置,接受 View。
Bar fitsStatusBarView(View view);
// 讓某一個 View 考慮導航欄的高度,顯示在適當的位置,接受 ViewId。
Bar fitsNavigationBarView(View view);
// 讓某一個 View 考慮導航欄的高度,顯示在適當的位置,接受 View。
Bar fitsNavigationBarView(View view);
注意:Sofia.with(Activity)
後返回一個Bar
接口實例,這裏的方法結合具體的場景調用一個或者幾個方法,也可以一個都不調用
最後,一些場景的方法使用
1、如圖
代碼爲:
Sofia.with(this)
.statusBarLightFont()//狀態欄淺色字體
.invasionStatusBar()//內容入侵狀態欄
.navigationBarBackground(ContextCompat.getColor(this, R.color.toast_bg))//導航欄背景色
.statusBarBackground(Color.TRANSPARENT);//狀態欄背景色
2、如圖,改變狀態欄和導航欄背景
代碼爲:
Sofia.with(this)
.statusBarBackground(ContextCompat.getDrawable(this, R.drawable.banner))//設置狀態欄背景Drawable
.navigationBarBackground(ContextCompat.getDrawable(this, R.drawable.icon_01));//設置導航欄背景Drawable