时间:2021-05-20
本文实例为大家分享了Android PopupWindow增加半透明蒙层的具体代码,供大家参考,具体内容如下
先看效果图:
实现代码:
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_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系 wl.token = token;//获取当前Activity中的View中的token,来依附Activity maskView = new View(context); maskView.setBackgroundColor(0x7f000000); maskView.setFitsSystemWindows(false); maskView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { removeMask(); return true; } return false; } }); /** * 通过WindowManager的addView方法创建View,产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。 * 比如创建系统顶级窗口,实现悬浮窗口效果! */ windowManager.addView(maskView, wl); } private void removeMask() { if (null != maskView) { windowManager.removeViewImmediate(maskView); maskView = null; } } @Override public void dismiss() { removeMask(); super.dismiss(); }}TestPopupWindow.class
package com.example.popupwindowwithmask; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager; /** * Created by kk on 2017/7/22. */ public class TestPopupWindow extends BasePopupWindowWithMask { private int[] mIds; private View contentView; private OnItemClickListener listener; public interface OnItemClickListener { void OnItemClick(View v); } public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } public TestPopupWindow(Context context, int[] mIds) { super(context); this.mIds = mIds; initListener(); } @Override protected View initContentView() { contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null, false); return contentView; } private void initListener() { for (int i = 0; i < mIds.length; i++) { contentView.findViewById(mIds[i]).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != listener) { listener.OnItemClick(v); } dismiss(); } }); } } @Override protected int initHeight() { return WindowManager.LayoutParams.WRAP_CONTENT; } @Override protected int initWidth() { return (int) (0.5 * UIUtils.getScreenWidth(context)); }}MainActivity.class
pop_layout.xml
pop_background.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <corners android:radius="5dp" /></shape>UIUtils.class
源码:下载地址
参考资料:
链接1
链接2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android编程实现popupwindow弹出后屏幕背景变成半透明效果的方法。分享给大家供大家参考,具体如下:android中popupwind
Android底部半透明弹出框PopUpWindow,供大家参考,具体内容如下layout布局:布局示意:代码部分:/**在当前页面调用initPopUpWin
本文实例为大家分享了popupWindow实现悬浮半透明效果的具体代码,供大家参考,具体内容如下如上图显示弹出一个半透明框java代码://清空数据privat
具体代码详情如下所示:基本思路先隐藏(dispaly:none)再显示,半透明蒙版层通过z-index:9998;z-index:9999;值越大越在前面ind
点击按钮,出现半透明遮罩层弹框,说说自己之前发过的愁吧1、遮罩层半透明了弹框也跟着半透明了就像这样绝望吧是哪里错了呢?你的css是这样写的吧:应该这样:需要注意