android中popupwindow,Android PopupWindow使用

最近项目中使用到了PopupWindow,记录下

334874da49ae?utm_campaign=hugo

效果图

刚开始是想用dialog来实现的,后面想了下最终还是用了PopupWindow来实现

首先使用的PopupWindow的地方比较多,用了一个管理类,代码如下:

package ***;

import android.view.View;

import android.view.ViewGroup;

/**

* Des: PopupWindow管理类

* Created by kele on 2020/7/21.

* E-mail:984127585@qq.com

*/

public class PopupWindowManager {

private MyPopupWindow pw;

/**

* 构造私有

*/

private PopupWindowManager() {

}

/**

* 匿名内部内实现单例

*/

private static class PopupWindowManagerHolder {

private static final PopupWindowManager INSTANCE = new PopupWindowManager();

}

public static PopupWindowManager getInstance() {

return PopupWindowManagerHolder.INSTANCE;

}

/**

* 初始宽度

*/

private static final int POP_WIDTH = ViewGroup.LayoutParams.MATCH_PARENT;

/**

* 初始高度

*/

private static final int POP_HEIGHT = ViewGroup.LayoutParams.WRAP_CONTENT;

/**

* 初始化 使用默认宽高

*

* @param contentView PopupWindow需要展示的view

* @return

*/

public PopupWindowManager init(View contentView) {

return init(contentView, POP_WIDTH, POP_HEIGHT);

}

/**

* 初始化 使用自定义宽高

*

* @param contentView PopupWindow需要展示的view

* @param width 宽

* @param height 高

* @return

*/

public PopupWindowManager init(View contentView, int width, int height) {

pw = new MyPopupWindow(contentView, width, height, true);

pw.setOutsideTouchable(true);

pw.setFocusable(true);

pw.setElevation(0);

pw.setTouchable(true);

return this;

}

/**

* 设置关闭监听

*

* @param listener

*/

public void setPopDismissListener(MyPopupWindow.OnDismissListener listener) {

if (null == pw) {

return;

}

pw.setOnDismissListener(listener);

}

/**

* 显示在指定的targetView的指定位置 无偏移量

*

* @param targetView 目标view

* @param gravity 位置

*/

public void showAtLocation(View targetView, int gravity) {

showAtLocation(targetView, gravity, 0, 0);

}

/**

* 显示在指定的targetView的指定位置 带偏移量

*

* @param targetView 目标view

* @param gravity 位置

* @param x x轴偏移量

* @param y y轴偏移量

*/

public void showAtLocation(View targetView, int gravity, int x, int y) {

if (null == pw) {

return;

}

pw.showAtLocation(targetView, gravity, x, y);

}

/**

* 显示在指定的targetView底部 无偏移量

*

* @param targetView 目标view

*/

public void showAsDropDown(View targetView) {

if (null == pw) {

return;

}

pw.showAsDropDown(targetView);

}

/**

* 显示在指定的targetView底部 有偏移量

*

* @param targetView 目标view

* @param offX x轴偏移量

* @param offY y轴偏移量

*/

public void showAsDropDown(View targetView, int offX, int offY) {

if (null == pw) {

return;

}

pw.showAsDropDown(targetView, offX, offY);

}

/**

* 显示在指定的targetView的指定位置

*

* @param targetView 目标view

* @param offX x轴偏移量

* @param offY y轴偏移量

* @param gravity 位置

*/

public void showAsDropDown(View targetView, int offX, int offY, int gravity) {

if (null == pw) {

return;

}

pw.showAsDropDown(targetView, offX, offY, gravity);

}

/**

* 关闭

*/

public void dismiss() {

if (null == pw) {

return;

}

pw.dismiss();

}

}

上面用到的 MyPopupWindow, 代码如下:

package ***;

import android.graphics.Rect;

import android.os.Build;

import android.view.View;

import android.widget.PopupWindow;

/**

* Des: 自定义PopupWindow

* Created by kele on 2020/7/21.

* E-mail:984127585@qq.com

*/

public class MyPopupWindow extends PopupWindow {

public MyPopupWindow(View contentView, int width, int height, boolean b) {

super(contentView, width, height, b);

}

@Override

public void showAsDropDown(View anchor) {

if (Build.VERSION.SDK_INT >= 24) {

Rect rect = new Rect();

anchor.getGlobalVisibleRect(rect);

int heightPixels = anchor.getResources().getDisplayMetrics().heightPixels;

int h = heightPixels - rect.bottom;

//设置负值能达到自适应的效果

setHeight(-20);

}

super.showAsDropDown(anchor);

}

}

注意:MyPopupWindow中的整体根布局的高度(如上图中上部分+带半透明黑色背景部分)需要在显示之前给设置一个小于-2的值才能达到宽度真正自适应的效果。这是经过多次的尝试后得到的结果,具体原因本人还未知,知道的可以留言告知下

具体使用

/**

* 显示

*

* @param targetView 需要显示的内容view

* @return

*/

public TimeSelectPopHolder show(View targetView) {

//显示pop

PopupWindowManager.getInstance().dismiss();

PopupWindowManager.getInstance().init(selectTime).showAsDropDown(targetView);

PopupWindowManager.getInstance().setPopDismissListener(new PopupWindow.OnDismissListener() {

@Override

public void onDismiss() {

if (null == mCallback) {

return;

}

mCallback.onDismiss();

}

});

return this;

}

更新-2020.09.14

更改下之前对PopupWindow高度问题的理解

1、本文上面说的关于高度自适应的问题,在我今天的撸码中出现了问题,和之前的理解有点不一样。今天的总结是:当你给pop设置了高度matchparent后,它不会出现在你要显示的view的下面,而是充满全屏,这个问题通过上面说的自定义重写pop的showAsDropDown方法是可以解决的,代码如下:

@Override

public void showAsDropDown(View anchor) {

if (Build.VERSION.SDK_INT >= 24) {

Rect rect = new Rect();

anchor.getGlobalVisibleRect(rect);

int heightPixels = anchor.getResources().getDisplayMetrics().heightPixels;

int h = heightPixels - rect.bottom;

setHeight(h);

}

super.showAsDropDown(anchor);

}

这样pop就可以在你需要显示的view下方以占据下半部分全部屏幕的方式来展现了!

总结下:pop关于高度下方充满屏幕,只需要重写showAsDropDown,设置写高度即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值