Android PopupWindow增加半透明蒙层
程序员文章站
2023-09-04 13:52:32
本文实例为大家分享了android popupwindow增加半透明蒙层的具体代码,供大家参考,具体内容如下
先看效果图:
实现代码:
basepopupwind...
本文实例为大家分享了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; } }
源码:下载地址
参考资料:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。