因為隨著全面屏時代的來臨,沉浸式的體驗對于APP變得越來越重要,PopupWindow作為APP一種重要的交互方式,如果不實現(xiàn)沉浸式的話,那么PopupWindow顯示時便會在狀態(tài)欄/系統(tǒng)導航欄/小白條上會出現(xiàn)丑陋的黑邊,或出現(xiàn)上下一邊有黑邊一邊沒有黑邊的情況,影響體驗。
但是,想要在Android中實現(xiàn)理想的沉浸式PopupWindow,并不是一件容易的事情,不僅Android不同版本的系統(tǒng)實現(xiàn)方式不同,而且Android提供的設置API也并不友好,大多數(shù)情況下,若我們想要達到理想的沉浸式PopupWindow,往往需要花費大量的時間。
由于每個項目都會或多或少遇到這個問題,解決起來也較為繁瑣,存在著大量樣板代碼,所以我做了一個開源Library項目,不僅能夠方便大家快速實現(xiàn)Android沉浸式PopupWindow,讓大家聚焦于業(yè)務功能代碼。
1、Library實現(xiàn)的功能
支持自定義PopupWindow背景顏色、系統(tǒng)欄圖標顏色、底部導航欄/小白條背景色等沉浸式配置
2、集成方式
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
// 注意,本項目基于androidx
implementation 'com.github.Arcns.arc-fast:immersive:1.23.1'
// Library中使用了Constraintlayout,如果你的項目中未引入,那么你還需要
implementation 'androidx.constraintlayout:constraintlayout:yourversion'
3、使用方式
第一步:PopupWindow改為繼承ImmersivePopupWindow
第二步:實現(xiàn)getImmersivePopupWindowConfig
/**
* 第一步:PopupWindow改為繼承ImmersivePopupWindow
*/
class TestPopupWindow : ImmersivePopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, 500) {
/**
* 第二步:實現(xiàn)getImmersivePopupWindowConfig(沉浸式配置),為簡化配置,我們內(nèi)置了三種常用配置:
* 1. createBottomPopupWindow 底部PopupWindows
* 2. createTopToAnchorBottomPopupWindow 頂部錨點PopupWindows(例如頂部下拉菜單)
* 3. createBottomToAnchorTopPopupWindow 底部錨點PopupWindows(例如底部上拉菜單)
* 如果您有更多自定義需求,您可以自行創(chuàng)建自己的ImmersivePopupWindowConfig
*/
override fun getImmersivePopupWindowConfig(context: Context) =
ImmersivePopupWindowConfig.createBottomPopupWindow(context)
}
4、ImmersivePopupWindowConfig支持的配置參數(shù)
| 配置參數(shù) | 類型 | 說明 |
|---|---|---|
| backgroundColor | Int | 背景顏色 |
| navigationColor | Int | 系統(tǒng)導航欄處/底部小白條的顏色 |
| canceledOnTouchOutside | Boolean | 觸摸PopupWindow之外的地方是否關閉PopupWindow |
| cancelable | Boolean | 點擊返回按鍵是否關閉PopupWindow |
| isLightStatusBarForegroundColor | Boolean | 系統(tǒng)狀態(tài)欄上的圖標與文字是否顯示為白色 |
| isLightNavigationBarForegroundColor | Boolean | 系統(tǒng)導航欄上的圖標是否顯示為白色 |
| backgroundConstraint | ImmersivePopupWindowBackgroundConstraint | 相對于錨點的背景布局約束 |
| enableBackgroundAnimator | Boolean | 是否啟用背景漸變動畫 |