通过对PopupWindow的封装,可以实现各个方向的弹窗,背景变暗等
特点:
- 带有相对于AnchorView的各个方位弹窗的办法。
- 支持PopupWindow弹出时整体背景变暗,制定背景变暗,变色等等
- 自定义PopupWindow,处理逻辑更清晰,操作更方便
PopupWindow与AlertDialog的区别:
最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
效果示意:
添加仓库依赖
allprojects {
repositories {
//...
maven {
url "https://jitpack.io" }
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
使用
1,基本使用
创建EasyPopup对象、
可以通过set属性()的方式进行属性设置,最后
通过createPopup()方法实现初始化
private EasyPopup initCirclePop;
...
private void initCirclePop() {
initCirclePop = = EasyPopup.create()
.setContentView(this, R.layout.layout_circle_comment)
.setAnimationStyle(R.style.RightPopAnim)
.setFocusAndOutsideEnable(true)
.apply();
通过设置.setFocusAndOutsideEnable()方法来决定点击PopupWindow以外的区域是否消失弹窗。
初始化View
通过对findViewById()的方法来获取新的view对象。
TextView tvZan=initCirclePop.findViewById(R.id.tv_zan);
TextView tvComment=initCirclePop.findViewById(R.id.tv_comment);
tvZan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showShort("很帅");
mCirclePop.dismiss();
}
});
tvComment