Popwindow学习笔记
工作中已经使用很多次popwindow了。但是一直没有好好总结,今天乘此机会把popwindow过一遍。在activity中我们可以选择使用popwindow和dialog弹出某一个小界面,但是默认情况下dialog是居中显示的,popwindow支持任何位置,先比较而言更新灵活。大致分一下几个方面吧。
一、popwindow的基本方法
二、popwindow使用实例
三、popwindow动画
四、popwindow几个方法解析
一、popwindow的基本方法
public PopupWindow() public PopupWindow(View contentView)public PopupWindow(int width, int height)public PopupWindow(View contentView, int width, int height)public PopupWindow(View contentView, int width, int height, boolean focusable)
平时我们使用最多的就是第三个构造函数。请注意,在使用popwindow时,有三要素,缺一不可,contextView、width、height。
再看看显示的方法。
pop.showAsDropDown(anchor);
pop.showAsDropDown(anchor, xoff, yoff);
pop.showAsDropDown(anchor, xoff, yoff, gravity);
pop.showAtLocation(parent, gravity, x, y);
显示在某一个控件下方。或者是基于某一个控件显示。其中偏移量要注意一下,x大于0,是向左边,x小于0,是向右边。y大于0是向上边,y小于0是向下边。
二、popwindow使用实例
1. 比如在屏幕的底部显示出来。核心代码我贴出。
View contentView = LayoutInflater.from(this).inflate(R.layout.pop, null);
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);View rootView = LayoutInflater.from(this).inflate(R.layout.main, null);
pop.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
我们来看看popwindow对应的布局
<?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" ><Viewandroid:layout_width="match_parent"android:layout_height="3dp"android:background="#ff0000" /><TextViewandroid:gravity="center"android:id="@+id/tv_study"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="学习" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#333333" /><TextViewandroid:gravity="center"android:id="@+id/tv_life"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="生活" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#333333" /><TextViewandroid:gravity="center"android:id="@+id/tv_work"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="工作" /></LinearLayout>
根布局LinearLayout是指定大小了。为啥在创建popwindow时还需要指定宽和高呢。后边说明。
2. 模仿diglog的效果,弹出popwindow时,背景是有阴影效果的。
View contentView = LayoutInflater.from(this).inflate(R.layout.pop2, null);
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setOutsideTouchable(true);
pop.showAsDropDown(btnShow);
详细代码可以下载。
三、popwindow动画
<style name="contextMenuAnim" parent="@android:style/Animation.Activity"><item name="android:windowEnterAnimation">@anim/context_menu_enter</item><item name="android:windowExitAnimation">@anim/context_menu_exit</item></style>
context_menu_enter文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="@android:integer/config_shortAnimTime"android:fromXDelta="0"android:fromYDelta="100%p"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:toXDelta="0"android:toYDelta="0"/></set>
context_menu_exit文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" ><translateandroid:duration="@android:integer/config_shortAnimTime"android:fromXDelta="0"android:fromYDelta="0"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:toXDelta="0"android:toYDelta="100%p" /></set>
通过代码pop.setAnimationStyle(R.style.contextMenuAnim);设置动画。
四、popwindow几个方法解析
<span style="font-family:Microsoft YaHei;">public void setTouchable(boolean touchable)
public void setFocusable(boolean focusable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)</span>
1. setTouchable
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
4. setBackgroundDrawable
Popwindow学习笔记相关推荐
- PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call
您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...
- 容器云原生DevOps学习笔记——第三期:从零搭建CI/CD系统标准化交付流程
暑期实习期间,所在的技术中台-效能研发团队规划设计并结合公司开源协同实现符合DevOps理念的研发工具平台,实现研发过程自动化.标准化: 实习期间对DevOps的理解一直懵懵懂懂,最近观看了阿里专家带 ...
- 容器云原生DevOps学习笔记——第二期:如何快速高质量的应用容器化迁移
暑期实习期间,所在的技术中台-效能研发团队规划设计并结合公司开源协同实现符合DevOps理念的研发工具平台,实现研发过程自动化.标准化: 实习期间对DevOps的理解一直懵懵懂懂,最近观看了阿里专家带 ...
- 2020年Yann Lecun深度学习笔记(下)
2020年Yann Lecun深度学习笔记(下)
- 2020年Yann Lecun深度学习笔记(上)
2020年Yann Lecun深度学习笔记(上)
- 知识图谱学习笔记(1)
知识图谱学习笔记第一部分,包含RDF介绍,以及Jena RDF API使用 知识图谱的基石:RDF RDF(Resource Description Framework),即资源描述框架,其本质是一个 ...
- 计算机基础知识第十讲,计算机文化基础(第十讲)学习笔记
计算机文化基础(第十讲)学习笔记 采样和量化PictureElement Pixel(像素)(链接: 采样的实质就是要用多少点(这个点我们叫像素)来描述一张图像,比如,一幅420x570的图像,就表示 ...
- Go 学习推荐 —(Go by example 中文版、Go 构建 Web 应用、Go 学习笔记、Golang常见错误、Go 语言四十二章经、Go 语言高级编程)
Go by example 中文版 Go 构建 Web 应用 Go 学习笔记:无痕 Go 标准库中文文档 Golang开发新手常犯的50个错误 50 Shades of Go: Traps, Gotc ...
- MongoDB学习笔记(入门)
MongoDB学习笔记(入门) 一.文档的注意事项: 1. 键值对是有序的,如:{ "name" : "stephen", "genda" ...
最新文章
- matplotlib hist 绘制直方图 bar 绘制条形图 API
- 利用sql报错帮助进行sql注入
- java swt 双屏_Java中AWT、Swing与SWT三大GUI技术的原理与效率差异
- 对于ssh和hadoop联系讲解和ssh的基本内容
- 智能搜索推荐一体化营收增长解决方案
- 压缩信息立方体和集合技术内幕
- 【转】extern “C“以及__declspec(dllexport) 讲解和def文件dll导出方法
- android图片缩小和放大Matrix
- c语言栈的实现以及操作_C++语言实现顺序栈
- 用户'MYCOMPUTER\ASPNET' 登录失败”的解决方法
- 关于random的多种用法
- grpc python 多进程_Python多进程通信Queue、Pipe、Value、Array实例
- 进阶篇第九期:相册与拍照的后处理
- Jacoco 实现 Android 端手工测试覆盖率统计
- CI520读卡芯片 软硬件兼容替换CV520
- python word2vec使用_使用Python可视化Word2vec的结果
- python数组冒号使用_python矩阵冒号
- Hadoop Streaming和Pipes会意
- Java 基础实验 自由落体
- centos离线安装(升级)nvidia显卡驱动及cuda10.2
热门文章
- 【python numpy】a.cumsum()、np.interp()、np.maximum.accumulate()、np.trapz()
- 合金轮毂售后市场现状研究分析与预测报告
- 求三个数最大公约数算法
- 华为+4x+410处理器+android+5.0,华为畅享5的CPU是什么?华为畅享5的CPU主频是多少?...
- ES 性能调优,这可能是全网最详细的 Elasticsearch 性能调优指南
- .net core .net6 Form Cookie Login 认证
- idea字体大小调整
- java Map转Object Object转Map
- css3 手机相册,CSS相册简单实现方法(功能分析及代码)
- 乱序和屏障1 : 总览 及 编译器内存屏障