Android沉浸式效果的實現—Sofia的使用

Android沉浸式效果的實現,狀態欄和導航欄均支持設置顏色、漸變色、圖片、透明度、內容入侵和狀態欄深色字體;兼容豎屏、橫屏,當屏幕旋轉時會自動適配。

現在我們的項目中經常需要適配StatusBarNavigationBar,我們需要寫很多的代碼來進行適配,已完成所需功能。現在來介紹一個第三方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

參考:http://p.codekk.com/detail/Android/yanzhenjie/Sofia

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章