因为随着全面屏时代的来临,沉浸式的体验对于APP变得越来越重要,PopupWindow作为APP一种重要的交互方式,如果不实现沉浸式的话,那么PopupWindow显示时便会在状态栏/系统导航栏/小白条
上会出现丑陋的黑边,或出现上下一边有黑边一边没有黑边的情况,影响体验。
但是,想要在Android中实现理想的沉浸式PopupWindow,并不是一件容易的事情,不仅Android不同版本的系统实现方式不同,而且Android提供的设置API也并不友好,大多数情况下,若我们想要达到理想的沉浸式PopupWindow,往往需要花费大量的时间。
由于每个项目都会或多或少遇到这个问题,解决起来也较为繁琐,存在着大量样板代码,所以我做了一个开源Library项目,不仅能够方便大家快速实现Android沉浸式PopupWindow,让大家聚焦于业务功能代码。
1、Library实现的功能
支持自定义PopupWindow背景颜色、系统栏图标颜色、底部导航栏/小白条背景色等沉浸式配置
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
第二步:实现getImmersivePopupWindowConfig
/**
* 第一步:PopupWindow改为继承ImmersivePopupWindow
*/
class TestPopupWindow : ImmersivePopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, 500) {
/**
* 第二步:实现getImmersivePopupWindowConfig(沉浸式配置),为简化配置,我们内置了三种常用配置:
* 1. createBottomPopupWindow 底部PopupWindows
* 2. createTopToAnchorBottomPopupWindow 顶部锚点PopupWindows(例如顶部下拉菜单)
* 3. createBottomToAnchorTopPopupWindow 底部锚点PopupWindows(例如底部上拉菜单)
* 如果您有更多自定义需求,您可以自行创建自己的ImmersivePopupWindowConfig
*/
override fun getImmersivePopupWindowConfig(context: Context) =
ImmersivePopupWindowConfig.createBottomPopupWindow(context)
}
4、ImmersivePopupWindowConfig支持的配置参数
配置参数 | 类型 | 说明 |
---|---|---|
backgroundColor | Int | 背景颜色 |
navigationColor | Int | 系统导航栏处/底部小白条的颜色 |
canceledOnTouchOutside | Boolean | 触摸PopupWindow之外的地方是否关闭PopupWindow |
cancelable | Boolean | 点击返回按键是否关闭PopupWindow |
isLightStatusBarForegroundColor | Boolean | 系统状态栏上的图标与文字是否显示为白色 |
isLightNavigationBarForegroundColor | Boolean | 系统导航栏上的图标是否显示为白色 |
backgroundConstraint | ImmersivePopupWindowBackgroundConstraint | 相对于锚点的背景布局约束 |
enableBackgroundAnimator | Boolean | 是否启用背景渐变动画 |