android 按钮加上蒙层,Android PopupWindow增加半透明蒙层

本文展示了如何在Android中为PopupWindow添加半透明蒙层,包括具体实现代码和使用示例,通过BasePopupWindowWithMask抽象类实现蒙层效果,并在TestPopupWindow中进行内容填充和事件监听。
摘要由CSDN通过智能技术生成

本文实例为大家分享了Android PopupWindow增加半透明蒙层的具体代码,供大家参考,具体内容如下

先看效果图:

adf0d49c7fc36b33e68c90f6c0052a76.png

实现代码:

BasePopupWindowWithMask.class

package com.example.popupwindowwithmask;

import android.content.Context;

import android.graphics.PixelFormat;

import android.graphics.drawable.ColorDrawable;

import android.os.IBinder;

import android.view.KeyEvent;

import android.view.View;

import android.view.WindowManager;

import android.widget.PopupWindow;

/**

* Created by kk on 2017/7/22.

*/

public abstract class BasePopupWindowWithMask extends PopupWindow {

protected Context context;

private WindowManager windowManager;

private View maskView;

public BasePopupWindowWithMask(Context context) {

super(context);

this.context = context;

windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

setContentView(initContentView());

setHeight(initHeight());

setWidth(initWidth());

setOutsideTouchable(true);

setFocusable(true);

setTouchable(true);

setBackgroundDrawable(new ColorDrawable());

}

protected abstract View initContentView();

protected abstract int initHeight();

protected abstract int initWidth();

@Override

public void showAsDropDown(View anchor) {

addMask(anchor.getWindowToken());

super.showAsDropDown(anchor);

}

private void addMask(IBinder token) {

WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

wl.width = WindowManager.LayoutParams.MATCH_PARENT;

wl.height = WindowManager.LayoutParams.MATCH_PARENT;

wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色

wl.type = WindowManager.LayoutParams.TYPE_APPLI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值