Android PopupWindow增加半透明蒙层
作者:爱dy 发布时间:2021-12-30 04:12:09
标签:Android,PopupWindow,蒙层
本文实例为大家分享了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
package com.example.popupwindowwithmask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_popup);
final TestPopupWindow testPopupWindow = new TestPopupWindow(this, new int[]{R.id.pop_location, R.id.pop_group, R.id.pop_list});
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testPopupWindow.showAsDropDown(textView);
}
});
testPopupWindow.setOnItemClickListener(new TestPopupWindow.OnItemClickListener() {
@Override
public void OnItemClick(View v) {
switch (v.getId()) {
case R.id.pop_location:
Toast.makeText(MainActivity.this, "地址", Toast.LENGTH_SHORT).show();
break;
case R.id.pop_group:
Toast.makeText(MainActivity.this, "分组", Toast.LENGTH_SHORT).show();
break;
case R.id.pop_list:
Toast.makeText(MainActivity.this, "清单", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
pop_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="12dp"
android:scaleType="fitCenter"
android:src="@drawable/filter_arrow_up" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_below="@+id/rl_indicator"
android:background="@drawable/pop_background"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:id="@+id/pop_location"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawableLeft="@mipmap/fault_equipment_location_icon"
android:drawablePadding="12dp"
android:gravity="center_vertical"
android:text="地址"
android:textColor="#000"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:background="#D2D2D2" />
<TextView
android:id="@+id/pop_group"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawableLeft="@mipmap/fault_equipment_grouping_icon"
android:drawablePadding="12dp"
android:gravity="center_vertical"
android:text="分组"
android:textColor="#000"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:background="#D2D2D2" />
<TextView
android:id="@+id/pop_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawableLeft="@mipmap/fault_equipment_list_icon"
android:drawablePadding="12dp"
android:gravity="center_vertical"
android:text="清单"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
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
package com.example.popupwindowwithmask;
import android.content.Context;
/**
* Created by kk on 2017/7/22.
*/
public class UIUtils {
/**
* 获得屏幕宽度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获得屏幕高度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
}
源码:下载地址
参考资料:
链接1
链接2
来源:https://blog.csdn.net/qq_33748378/article/details/75935935?utm_source=blogxgwz4


猜你喜欢
- 引入线程是为了减少程序在并发执行时所付出的时空开销。属性:轻型实体。它不拥有系统资源,只是有一点必不可少的、能保证独立运行的资源。独立调度和
- Service的生命周期 (适用于2.1及以上)1. 被startService的无论是否有任何活动绑定到该Service,都在后台运行。o
- 本文为大家分享了Android Studio3安装图文教程,供大家参考,具体内容如下Android Studio及其相关资源下载地址:dow
- RecyclerView上拉加载,先看效果:网上有很多这类得框架,不过在自己的项目只用到上拉加载的功能,所以自己封装一个简单点的。主要依赖B
- 刚开始项目,需要用到mybatis分页,网上看了很多插件,其实实现原理基本都大同小异,但是大部分都只给了代码,注释不全,所以参考了很多篇文章
- 一、前言什么是多渠道打包以及多渠道打包可以做什么,这里就不做介绍了,相信看到这篇文章的你已经了解了,多渠道打包的方式比较多,这里我们用Gra
- GoogleNow是Android4.1全新推出的一款应用他,它可以全面了解你的使用习惯,并为你提供现在或者未来可能用到的各种信息,Goog
- 先看一下Android悬浮按钮点击回到顶部的效果:FloatingActionButton是Design Support库中提供的一个控件,
- 欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demo
- ActiveMQ是Apache的一个开源项目,它是一个功能强劲的开源消息总线,也是一个中间件产品,它是JMS的一个实现。在介绍ActiveM
- 创建类第一步新建一个java类QSV,构造函数传入需要解析的文件名称。public class QSV {private RandomAcc
- 前言日志模块是每个项目中必须的,用来记录程序运行中的相关信息。一般在开发环境下使用DEBUG级别的日志输出,为了方便查看问题,而在线上一般都
- 后台服务端import java.io.IOException;import java.io.InputStream;import java
- 解决Spring in action @valid验证不生效按照书上的示例代码来实现但是,添加了验证但是没有生效。Spring提供了校验Ap
- 场景最近在做数据分析项目,里面有这样一个业务:把匹配的数据打上标签,放到新的索引中。数据量:累计亿级的数据使用场景:可能会单次查询大量的数据
- 本文实例为大家分享了C# Email发送邮件的具体代码,供大家参考,具体内容如下//回执地址 var Receipt = &q
- 本文实例讲述了java实现给出分数数组得到对应名次数组的方法。分享给大家供大家参考。具体实现方法如下:package test01;/**
- 前言提前说明下:(该方法只适用于监控自己拥有的微信或者QQ ,无法监控或者盗取其他人的聊天记录。本文只写了如何获取聊天记录,服务器落地程序并
- 本文实例讲述了Java利用反射自动封装成实体对象的方法。分享给大家供大家参考。具体分析如下:利用此方法的时候需要传递的参数的名称,必须以行号
- Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建议大家使用S