Android PopupWindow使用,下拉式PopupWindow,底部式PopupWindow

1、实现方法1                                        仿微信盆友圈弹出点赞、评论

   

demo连接:android开发PopupWindow实现跟随试弹出框-Android文档类资源-CSDN下载

实现步骤

1、下载module并引入项目

引入module步骤:Android studio 导入module方法_meixi_android的博客-CSDN博客_as怎么导入module

2、实现方法2

1、封装弹出窗类

/*** 作者:created by meixi* 邮箱:13164716840@163.com* 日期:2018/10/26 15*/public abstract class CommonPopupWindow {protected Context context;protected View contentView;protected PopupWindow mInstance;public CommonPopupWindow(Context c, int layoutRes, int w, int h) {context=c;contentView= LayoutInflater.from(c).inflate(layoutRes, null, false);initView();initEvent();mInstance=new PopupWindow(contentView, w, h, true);initWindow();}public View getContentView() { return contentView; }public PopupWindow getPopupWindow() { return mInstance; }protected abstract void initView();protected abstract void initEvent();protected void initWindow() {mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));mInstance.setOutsideTouchable(true);
//        mInstance.setTouchable(true);}public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {int[] offset=layoutGravity.getOffset(anchor, mInstance);mInstance.showAsDropDown(anchor, offset[0]+xmerge, offset[1]+ymerge);}public void showAsDropDown(View anchor, int xoff, int yoff) {mInstance.showAsDropDown(anchor, xoff, yoff);}public void showAtLocation(View parent, int gravity, int x, int y) {mInstance.showAtLocation(parent, gravity, x, y);}public static class LayoutGravity {private int layoutGravity;// waring, don't change the order of these constants!public static final int ALIGN_LEFT=0x1;public static final int ALIGN_ABOVE=0x2;public static final int ALIGN_RIGHT=0x4;public static final int ALIGN_BOTTOM=0x8;public static final int TO_LEFT=0x10;public static final int TO_ABOVE=0x20;public static final int TO_RIGHT=0x40;public static final int TO_BOTTOM=0x80;public static final int CENTER_HORI=0x100;public static final int CENTER_VERT=0x200;public LayoutGravity(int gravity) {layoutGravity=gravity;}public int getLayoutGravity() { return layoutGravity; }public void setLayoutGravity(int gravity) { layoutGravity=gravity; }public void setHoriGravity(int gravity) {layoutGravity&=(0x2+0x8+0x20+0x80+0x200);layoutGravity|=gravity;}public void setVertGravity(int gravity) {layoutGravity&=(0x1+0x4+0x10+0x40+0x100);layoutGravity|=gravity;}public boolean isParamFit(int param) {return (layoutGravity & param) > 0;}public int getHoriParam() {for(int i=0x1; i<=0x100; i=i<<2)if(isParamFit(i))return i;return ALIGN_LEFT;}public int getVertParam() {for(int i=0x2; i<=0x200; i=i<<2)if(isParamFit(i))return i;return TO_BOTTOM;}public int[] getOffset(View anchor, PopupWindow window) {int anchWidth=anchor.getWidth();int anchHeight=anchor.getHeight();int winWidth=window.getWidth();int winHeight=window.getHeight();View view=window.getContentView();if(winWidth<=0)winWidth=view.getWidth();if(winHeight<=0)winHeight=view.getHeight();int xoff=0;int yoff=0;switch (getHoriParam()) {case ALIGN_LEFT:xoff=0; break;case ALIGN_RIGHT:xoff=anchWidth-winWidth; break;case TO_LEFT:xoff=-winWidth; break;case TO_RIGHT:xoff=anchWidth; break;case CENTER_HORI:xoff=(anchWidth-winWidth)/2; break;default:break;}switch (getVertParam()) {case ALIGN_ABOVE:yoff=-anchHeight; break;case ALIGN_BOTTOM:yoff=-winHeight; break;case TO_ABOVE:yoff=-anchHeight-winHeight; break;case TO_BOTTOM:yoff=0; break;case CENTER_VERT:yoff=(-winHeight-anchHeight)/2; break;default:break;}return new int[]{ xoff, yoff };}}
}

外部不可点击

mInstance.setOutsideTouchable(false);
mInstance.setFocusable(false);

2、activity调用代码

(1)下拉式弹出

private CommonPopupWindow.LayoutGravity layoutGravity;private CommonPopupWindow window;
private View activityPopup;
activityPopup=findViewById(R.id.activity_popup);
        textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {PopupWindow win=window.getPopupWindow();win.setAnimationStyle(R.style.animTranslate);//动画效果window.showBashOfAnchor(textView, layoutGravity, 0, 0);//跟随式弹出
//                window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0);//底部弹出WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=0.3f;getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});

下拉式实例化

private void initPopup2(){layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.CENTER_HORI| CommonPopupWindow.LayoutGravity.TO_BOTTOM);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int nMultiple = dm.widthPixels;//宽window=new CommonPopupWindow(this, R.layout.mypopup, (int) (nMultiple*0.4), ViewGroup.LayoutParams.WRAP_CONTENT) {@Overrideprotected void initView() {View view=getContentView();linearLayout =(LinearLayout)view.findViewById(R.id.oneli);}@Overrideprotected void initEvent() {linearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"ssssblue",Toast.LENGTH_SHORT).show();}});}@Overrideprotected void initWindow() {super.initWindow();PopupWindow instance=getPopupWindow();instance.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=1.0f;getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});}};
}

(2)底部弹出

    private void initPopupWindow() {DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int nMultiple = dm.widthPixels;//宽//        window=new CommonPopupWindow(this, R.layout.mypopup, ViewGroup.LayoutParams.MATCH_PARENT, (int) (screenHeight*0.7)) {window=new CommonPopupWindow(this, R.layout.mypopup, (int) (nMultiple*0.9), ViewGroup.LayoutParams.WRAP_CONTENT) {@Overrideprotected void initView() {View view=getContentView();linearLayout =(LinearLayout)view.findViewById(R.id.oneli);}@Overrideprotected void initEvent() {linearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"ssssblue",Toast.LENGTH_SHORT).show();}});}@Overrideprotected void initWindow() {super.initWindow();PopupWindow instance=getPopupWindow();instance.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=1.0f;getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});}};}

  window.getPopupWindow().dismiss();//隐藏

demo链接:PopupWindow实现demo-Android代码类资源-CSDN下载

下拉式弹出窗                                              全屏底部弹出窗

         

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415044.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android 微信登录

/1、首先你得到微信.开放平台申请开发权限&#xff1a;https://open.weixin.qq.com/ 申请通过效果如下即可 2、其次&#xff0c;阅读开发文档&#xff1a; https://open.weixin.qq.com/cgi-bin/showdocument?actiondir_list&tresource/res_list&verify1&idopen1…

解决ubuntu16.04 qt5.9.1无法输入中文

1. 安装 fcitx-frontend-qt5 sudo apt-get install fcitx-frontend-qt5 2. 确认该路径下存在的文件 /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so 3. 将libfcitxplatforminputcontextplugin.so 复制到以下两个路径下&…

Bitmap添加文字水印

private static Bitmap AddTimeWatermark(Bitmap mBitmap) {//获取原始图片与水印图片的宽与高int mBitmapWidth mBitmap.getWidth();int mBitmapHeight mBitmap.getHeight();Bitmap mNewBitmap Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888)…

工作200:视频上传和图片编辑功能

1眼睛一定要看清 本地接口 稳得很

highcharts x轴 按照时间 datetime排序

1、我的配置文件代码&#xff1a; var chart Highcharts.chart(warningCharts, {chart: {type: line,},style: {fontSize: 12px,color: #006cee,padding: 10rpx,},title: {text: null,},subtitle: {text: null,},series: data,xAxis: {type: datetime,dateTimeLabelFormats: …

B. 重载技术(overloading)

转载于:https://www.cnblogs.com/youyuanjuyou/p/8120968.html

highcharts默认选中最后一个点数据

效果图如下&#xff1a; 核心代码如下&#xff1a; chart: {type: line,events: {load: function () {let chart thisconst points []Highcharts.each(chart.series, function (s) {if (s.visible) {points.push(s.points[s.points.length - 1])}})chart.tooltip.refresh(p…

上传Android应用到腾讯应用宝,乐固加固应用使用

当我们开发完安卓系统APP之后。需要上传到应用市场 在上传到腾讯应用宝是&#xff0c;需要使用腾讯加固工具 乐固 加固apk。才能正常上架。 如没有加固应用会提示&#xff1a; 加固步骤: 1、下载乐固包&#xff1a;https://download.csdn.net/download/meixi_android/107534…

工作203:实现预览效果

1子组件 成功 ChangeRest(event, file){/* console.log(event)console.log(file)*//* console.log(URL.createObjectURL(file.raw))*/this.imageUrl URL.createObjectURL(file.raw);/* this.imageUrlevent.tmp_urlconsole.log(this.imageUrl)*/console.log(1)this.eventSav…

【深入Java虚拟机】之一:Java内存区域与内存溢出

内存区域 Java虚拟机在执行Java程序的过程中会把他所管理的内存划分为若干个不同的数据区域。Java虚拟机规范将JVM所管理的内存分为以下几个运行时数据区&#xff1a;程序计数器、Java虚拟机栈、本地方法栈、Java堆、方法区。下面详细阐述各数据区所存储的数据类型。 程序计数器…

Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK

安装失败&#xff0c;gradle.properties文件下添加 即可 android.injected.testOnly false shell命令安装 /*** 安装apk** param path apk文件路径*/ public void installAPK(String path) {Log.i(TAG, "installAPK:" path);com.dlc.xiaohaitun.utils.ShellUtil…

Android 自定义Switch,仿微信开关键Switch

switch (changeFragmentEvent.getViewId()) {//启动设备&#xff08;安卓接收&#xff09;case 1:textView.setText("vvvvv555"changeFragmentEvent.getstring());break;case 2:textView.setText("vvvvv555WWW22");break; } 效果 实现方法 1、drawable下创…

工作207:修改表头按钮样式

<el-card><div style"display: flex;justify-content: space-between"><h1 style"float: left;margin-top: 34px;margin-left: 32px;">我的任务</h1><el-button style"float: right;margin-top: 14px;margin-right: 10p…

Android 带阴影背景图片

1、添加依赖 compile com.dingmouren.paletteimageview:paletteimageview:1.0.7 2、引用 <com.dingmouren.paletteimageview.PaletteImageViewandroid:id"id/palette"android:layout_width"400dp"android:layout_height"400dp"android:lay…

Android画板控件,可以写字,签名,画画并生成图片

1效果图 实现步骤 1、添加画板控件module 画板控件module下载&#xff1a;https://download.csdn.net/download/meixi_android/10774781 2、xml文件 <?xml version"1.0" encoding"utf-8"?> <LinearLayoutandroid:id"id/content_main&q…

工作209:整理订单的重置逻辑

1点击新增 2找到子组件下面的混入 3找到混入 4这一块或许就是重置逻辑