PopWindow是一个可以在Activity 之上显示任意View的控件。
PopWindow可自定义进出动画、显示位置,根据已有视图对齐显示,可谓弹框一哥。
实例:在按钮上面显示
一、创建及配置
加载视图-->创建对象-->初始设置
1.代码
//1.加载视图
View popView = LayoutInflater.from(this).inflate(R.layout.pop_show, null);
//2.创建对象
mPopupWindow = new PopupWindow();
//3.初始设置
mPopupWindow.setContentView(popView);//设置视图
mPopupWindow.setWidth(300);//设置宽
mPopupWindow.setHeight(500);//设置高
// //等价于上面四句
// mPopupWindow = new PopupWindow(popView,
// LinearLayout.LayoutParams.MATCH_PARENT,
// LinearLayout.LayoutParams.MATCH_PARENT);
mPopupWindow.setFocusable(true);//可聚焦--默认true
mPopupWindow.setTouchable(true);//可触碰--默认true
mPopupWindow.setOutsideTouchable(true);//点外部消失
//在安卓4.4(API 19)测试需要背景(这里是一个透明色)才能点外部消失,安卓8.1(API 27)则不需要
mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.color.transparent));
//4.设置动画样式:
mPopupWindow.setAnimationStyle(R.style.slide_anim);//设置mPopWindow进出动画
2.动画样式:
默认动画是忽然出现,忽然消失的
values/styles.xml
<style name="slide_anim">
<item name="android:windowEnterAnimation">@anim/slide_up</item>
<item name="android:windowExitAnimation">@anim/slide_down</item>
</style>
anim/slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--划下:-->
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="100%"/>
</set>
anim/slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!--划上:-->
<translate
android:duration="200"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
二、显示与隐藏
//显示的适配
CompatShowPop.handle(mPopupWindow, mButton, CompatShowPop.BOTTOM2TOP);
mPopupWindow.dismiss();//消失
/**
* 作者:张风捷特烈<br/>
* 时间:2018/11/1 0001:10:40<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:以View确定popWindow适配
*/
public class CompatShowPop {
//pop顶对顶
public static final int TOP2TOP = 0x01;
//pop顶对底
public static final int TOP2BOTTOM = 0x02;
//pop底对顶
public static final int BOTTOM2TOP = 0x03;
/**
* 以View确定popWindow适配
*
* @param popupWindow popupWindow
* @param view view
* @param type 类型
*/
public static void handle(PopupWindow popupWindow, View view, int type) {
//处理PopWindow7.0适配
if (Build.VERSION.SDK_INT < 24) {
switch (type) {
case TOP2TOP:
popupWindow.showAsDropDown(view, 0, -view.getHeight());
break;
case TOP2BOTTOM:
popupWindow.showAsDropDown(view, 0, 0);
break;
case BOTTOM2TOP:
// popupWindow.showAsDropDown(view, 0, -view.getHeight() - popupWindow.getHeight());
popupWindow.showAtLocation(view, Gravity.CLIP_HORIZONTAL, 0, 0);
}
} else {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
switch (type) {
case TOP2TOP:
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
break;
case TOP2BOTTOM:
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y + view.getHeight());
break;
case BOTTOM2TOP:
// popupWindow.showAtLocation(view, Gravity.CLIP_HORIZONTAL, 0, 0);
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y - popupWindow.getHeight());
}
}
}
}
三、PopupWindow的简单封装
/**
* 作者:张风捷特烈<br/>
* 时间:2018/11/1 0001:11:42<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:PopupWindow的封装
*/
public abstract class BasePopWindow extends PopupWindow {
private View mRootView;
private Context mContext;
private SparseArray<View> mViews;
public BasePopWindow(Context context) {
this(context, -1);
}
public BasePopWindow(Context context, int animStyleId) {
mContext = context;
//获取屏幕尺寸
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mRootView = LayoutInflater.from(context).inflate(layoutId(), null);
mViews = new SparseArray<>();
//基本设置
setContentView(mRootView);
setWidth(outMetrics.widthPixels);
setHeight(outMetrics.heightPixels);
setFocusable(true);
setTouchable(true);
setOutsideTouchable(true);
//沉浸标题栏,全屏显示
setClippingEnabled(false);
setBackgroundDrawable(new BitmapDrawable());
if (animStyleId != -1) {
setAnimationStyle(animStyleId);//设置mPopWindow进出动画
}
}
/**
* 通过viewId获取控件
*
* @param viewId pop里的控件id
* @param <T> 控件视图
* @return 控件视图
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mRootView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public abstract int layoutId();
}
后记:捷文规范
1.本文成长记录及勘误表
项目源码 | 日期 | 备注 |
---|---|---|
V0.1--无 | 2018-11-1 | 4-VIV-Android之PopupWindow |
2.更多关于我
笔名 | 微信 | 爱好 | |
---|---|---|---|
张风捷特烈 | 1981462002 | zdl1994328 | 语言 |
我的github | 我的简书 | 我的CSDN | 个人网站 |
3.声明
1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
4----看到这里,我在此感谢你的喜欢与支持