从下方弹出的PopupWindow,仿iphone效果,增加半透明蒙层。

亲测可用.
public abstract class BottomPushPopupWindow extends PopupWindow {

protected Context context;
private WindowManager wm;
private View maskView;

@SuppressWarnings("deprecation")
public BottomPushPopupWindow(Context context, T t) {
    super(context);
    this.context = context;
    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    initType();
    setContentView(generateCustomView(t));
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    setOutsideTouchable(true);
    setFocusable(true);
    setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
    setAnimationStyle(R.style.Animations_BottomPush);
}

protected abstract View generateCustomView(T t);

@TargetApi(23)
private void initType() {
    setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}

@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
    addMaskView(parent.getWindowToken());
    super.showAtLocation(parent, gravity, x, y);
}

@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
    addMaskView(anchor.getWindowToken());
    super.showAsDropDown(anchor, xoff, yoff);
}

@Override
public void dismiss() {
    removeMaskView();
    super.dismiss();
}

/**
 * 显示在界面的底部
 */
public void show(Activity activity) {
    showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}

private void addMaskView(IBinder token) {
    WindowManager.LayoutParams p = new WindowManager.LayoutParams();
    p.width = WindowManager.LayoutParams.MATCH_PARENT;
    p.height = WindowManager.LayoutParams.MATCH_PARENT;
    p.format = PixelFormat.TRANSLUCENT;
    p.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
    p.token = token;
    p.windowAnimations = android.R.style.Animation_Toast;
    maskView = new View(context);
    maskView.setBackgroundColor(0x7f000000);
    maskView.setFitsSystemWindows(false);
    maskView.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                removeMaskView();
                return true;
            }
            return false;
        }
    });
    wm.addView(maskView, p);
}

private void removeMaskView() {
    if (maskView != null) {
        wm.removeViewImmediate(maskView);
        maskView = null;
    }
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 要实现类似微信聊天消息长按菜单的效果,可以使用 PopupWindow 组件来实现。下面是实现的大致步骤: 1. 在布局文件中定义一个包含要的菜单项的布局文件,例如一个 LinearLayout。 2. 在代码中创建 PopupWindow 对象,并设置其内容为上一步中定义的布局文件。 3. 设置 PopupWindow 的大小、位置、背景等属性。 4. 监听长按事件,在长按事件中 PopupWindow。 5. 监听 PopupWindow 中菜单项的点击事件,处理相应的逻辑。 具体实现细节可以参考以下代码: ``` // 定义 PopupWindow PopupWindow popupWindow = new PopupWindow(context); View contentView = LayoutInflater.from(context).inflate(R.layout.menu_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 监听长按事件 view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 显示 PopupWindow int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight()); return true; } }); // 监听菜单项的点击事件 TextView menuItem = contentView.findViewById(R.id.menu_item); menuItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理菜单项的逻辑 popupWindow.dismiss(); } }); ``` 希望这个回答能够帮到你,如果还有其他问题,可以继续问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值