自定义PopupWindow
一、布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="#ffffff"android:padding="20dp" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:gravity="center"android:textColor="@android:color/holo_orange_dark"android:text="确定" /><TextViewandroid:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:clickable="true"android:gravity="center"android:text="取消" /></LinearLayout>
二、自定义MypopupWindow继承PopupWindow
public class MyPopupWindow extends PopupWindow {
三、重写构造方法与动画样式
在styles.xml自定义样式,动画
<style name="MyPopupWindow"><item name="android:windowEnterAnimation">@anim/pop_in</item><item name="android:windowExitAnimation">@anim/pop_out</item></style>
pop_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><!-- 平移<translateandroid:duration="5000"android:fromXDelta="100%"android:toXDelta="0"/>--><scaleandroid:fromXScale="0"android:fromYScale="0"android:pivotX="50%"android:pivotY="50%"android:toXScale="0.8"android:toYScale="0.5"android:duration="200"/><!-- fromXScale fromYScale 起始时X,Y座标,pivotX pivotY动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始toXScale toYScale 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大duration 动画持续时间--><!--透明度--><alphaandroid:duration="200"android:fromAlpha="0.0"android:toAlpha="1.0"/></set>
pop_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><!-- <translateandroid:duration="5000"android:fromXDelta="0"android:toXDelta="100%"/>--><scaleandroid:fromXScale="0.8"android:fromYScale="0.5"android:pivotX="50%"android:pivotY="50%"android:toXScale="0"android:toYScale="0"android:duration="200"/><alphaandroid:duration="200"android:fromAlpha="1.0"android:toAlpha="0.0"/></set>
四、重写构造方法并设置点击外部可以消失监听
super(context);this.mContext=context;//打气筒mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//打气 mContentView = mInflater.inflate(R.layout.layout_dialog,null);//设置View setContentView(mContentView);//设置宽与高 setWidth(WindowManager.LayoutParams.MATCH_PARENT);setHeight(WindowManager.LayoutParams.WRAP_CONTENT);/*** 设置进出动画*/setAnimationStyle(R.style.MyPopupWindow);/*** 设置背景只有设置了这个才可以点击外边和BACK消失*/setBackgroundDrawable(new ColorDrawable());/*** 设置可以获取集点*/setFocusable(true);/*** 设置点击外边可以消失*/setOutsideTouchable(true);/***设置可以触摸*/setTouchable(true);/*** 设置点击外部可以消失*/setTouchInterceptor(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {/*** 判断是不是点击了外部*/if(event.getAction()==MotionEvent.ACTION_OUTSIDE){return true;}//不是点击外部return false;}});
五、显示及设置窗口变暗与变亮
public void displayDialog(View view){MyPopupWindow myPopupWindow = new MyPopupWindow(this);myPopupWindow.showAsDropDown(mBtnDispaly,0,0);lightOff();/*** 消失时屏幕变亮*/myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams layoutParams = getWindow().getAttributes();layoutParams.alpha=1.0f;getWindow().setAttributes(layoutParams);}});}/*** 显示时屏幕变暗*/private void lightOff() {WindowManager.LayoutParams layoutParams = getWindow().getAttributes();layoutParams.alpha=0.3f;getWindow().setAttributes(layoutParams);}
六、完整
package liu.basedemo.view;import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow;import liu.basedemo.R;/*** 学习PopupWindow* Created by 刘楠 on 2016/8/1 0001.17:42*/ public class MyPopupWindow extends PopupWindow {Context mContext;private LayoutInflater mInflater;private View mContentView;public MyPopupWindow(Context context) {super(context);this.mContext=context;//打气筒mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//打气 mContentView = mInflater.inflate(R.layout.layout_dialog,null);//设置View setContentView(mContentView);//设置宽与高 setWidth(WindowManager.LayoutParams.MATCH_PARENT);setHeight(WindowManager.LayoutParams.WRAP_CONTENT);/*** 设置进出动画*/setAnimationStyle(R.style.MyPopupWindow);/*** 设置背景只有设置了这个才可以点击外边和BACK消失*/setBackgroundDrawable(new ColorDrawable());/*** 设置可以获取集点*/setFocusable(true);/*** 设置点击外边可以消失*/setOutsideTouchable(true);/***设置可以触摸*/setTouchable(true);/*** 设置点击外部可以消失*/setTouchInterceptor(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {/*** 判断是不是点击了外部*/if(event.getAction()==MotionEvent.ACTION_OUTSIDE){return true;}//不是点击外部return false;}});/*** 初始化View与监听器*/initView();initListener();}private void initView() {}private void initListener() {}}
转载于:https://www.cnblogs.com/liunanjava/p/5726977.html
自定义PopupWindow相关推荐
- 干货一:通过自定义PopupWindow实现QQ菜单选项功能
概述 我们在使用手机QQ时,点击菜单键,会弹出如本案例说演示的效果图似的菜单选项. 实现方式有很多种,在这里我们来演示下如何通过自定义PopupWindow的方式一步一步的实现如上效果. 关于Popu ...
- 自定义PopupWindow弹出后背景灰色状态
最近有做fragment里弹出自定义popupWindow, fragment里面调用: // 点击加号按钮 @Click protected void ll_add_pharmacy() { mPo ...
- android 自定义弹窗diss,Android中自定义PopupWindow,动态弹窗。
我的第一篇博客,咱们直奔主题.先上个效果图 在android中自定义PopupWindow: 1.首先定义好你想要显示的窗口的布局文件,再实例化一个View对象:窗口布局可灵活变化,dialog_la ...
- android popupwindowd弹出时后面变灰色,自定义PopupWindow弹出后背景灰色状态
最近有做fragment里弹出自定义popupWindow, fragment里面调用: // 点击加号按钮 @Click protected void ll_add_pharmacy() { mPo ...
- android 自定义 popupwindow,Android自定义弹出窗口PopupWindow使用技巧
PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(View contentView, int widt ...
- 自定义PopupWindow 怎么设置PopupWindow的宽度充满全屏宽度
自定义了一个MyPopMenu类,用于上图中的下拉筛选效果的. 但是按照网上有说需要: new PopupWindow(view,getWindowManager().getDefaultDispla ...
- android 自定义popupwindow,自定义通过PopupWindow实现通用菜单
会经常用户到菜单选项提供给用户选择,例如选择图片,图库和相机选择等一系列场景吧,根据为了以后更加方便使用通过自定义封装了一个菜单,主要是通过一个列表展示,将菜单项列表传入设置参数就可以显示,使用方便简 ...
- android 自定义popupwindow样式,自定义popupwindow组件
先看效果...... 现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的 上源码 main.xml android:layout_width ...
- 自定义PopupWindow全解
干货开始: 第一步: private PopupWindow windowpx, 定义全局PupupWindow. 第二步: <?xml version="1.0" enco ...
最新文章
- Centos 7.5安装配置MongoDB 4.0.4
- okhttputils java_Java OkHttpUtils.post方法代码示例
- 非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!
- [UI界面]-UIWindow
- centos7.5 mysql5.7 的卸载和离线安装全过程
- GithubPage自定义腾讯404界面
- Go 性能优化技巧 8/10 1
- MTK:内存管理机制简单分析
- 卡饭里的云计算机,微云可以在电脑用吗
- Kylin 与 Spark SQL相比,有哪些差异和优势?
- 【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!
- Clover 文件结构
- 不加群提取群成员_QQ群排名优化技术教程
- 林子雨-2.3 面向对象编程基础
- average函数python_在Python3 numpy中mean和average的区别详解
- 一些常用的英文写作网站
- 【转】右键菜单大揭密
- 羊le个羊 小游戏 简单源码开源 思路分析
- 新手级别的小程序【看完这个博客,小程序就入门了】
- 全球及中国建筑机械LED灯行业销售现状及竞争战略建议报告2021年版
热门文章
- JavaScript核心语法总结
- 一千个不用 Null 的理由,你还用?
- 足够应付面试的Spring事务源码阅读梳理
- Redis 从入门到起飞(上)
- linux gcc编译错误:statically linked applications requires at runtime the shared libraries
- SpringAOP静态代理和动态代理
- 3d地球旋转html,echarts 3D地球实现自动旋转
- 四核处理器_(技术文档)你知道AMD Ryzen处理器中的CCX与CCD是什么吗?
- php 7.1.5,Centos 7平滑无缝更新PHP7.1.0到PHP 7.1.5
- 某大型数据中心离心式冷水机组 控制板UPS电源改造项目案例分享