PopupWindow在项目中的使用 并指定位置及加入动画效果

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

项目做到一期收尾阶段,突然要用到弹出窗口,于是自然而然的就想起了PopupWindow这个组件了,才发现平时用的少,对这个组件几乎是完全无知的状态啊。
于是恶补一番
现在放出学习成果——————请无视展示效果

救我理解来看,PopupWindow 其实关键来说就是个Window容器,既然是容器,那么必然有他自己的宽高,但是内容就由着开发者自己定义了。

所以第一步 先定义两个变量 
    
  1. PopupWindow pop;//弹出窗口的容器
  2. View view; //容器内放置的内容

然后我们来看初始化方法:

    
  1. private void initPopupWindow(String json) {
  2. if (json == null || json.equals("")) {
  3. ToastUtil.WarnImageToast(Phone_MainActivity.this, "今日油价获取失败",
  4. "short");
  5. return;
  6. }
  7. String oil_text_90;
  8. String oil_text_93;
  9. String oil_text_97;
  10. String oil_text_0;
  11. OilBean ob = gson.fromJson(json, OilBean.class);
  12. oil_text_0 = ob.getShowapi_res_body().getList().get(0).getP0();
  13. oil_text_90 = ob.getShowapi_res_body().getList().get(0).getP90();
  14. oil_text_93 = ob.getShowapi_res_body().getList().get(0).getP93();
  15. oil_text_97 = ob.getShowapi_res_body().getList().get(0).getP97();
  16. view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
  17. CustomRLayout oil90 = (CustomRLayout) view
  18. .findViewById(R.id.oil_90_layout);
  19. CustomRLayout oil93 = (CustomRLayout) view
  20. .findViewById(R.id.oil_93_layout);
  21. CustomRLayout oil97 = (CustomRLayout) view
  22. .findViewById(R.id.oil_97_layout);
  23. CustomRLayout oil0 = (CustomRLayout) view
  24. .findViewById(R.id.oil_0_layout);
  25. oil90.setRightText("¥" + oil_text_90);
  26. oil93.setRightText("¥" + oil_text_93);
  27. oil97.setRightText("¥" + oil_text_97);
  28. oil0.setRightText("¥" + oil_text_0);
  29. pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
  30. ViewGroup.LayoutParams.WRAP_CONTENT);
  31. pop.setAnimationStyle(R.style.popwin_anim_style);
  32. pop.setOutsideTouchable(false);
  33. view.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. // TODO Auto-generated method stub
  37. pop.dismiss();
  38. }
  39. });
  40. }
整个过程非常简单:
首先判断传入的网络json是有数据的,然后对Json数据进行解析,将解析后的数据放入到指定的View的组件中去。
注意观察可以发现,在这个方法中用到了一个布局 R.layout.popup_window
我们来看看这个布局吧:
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/com.dhcc.gpscarmanager_phone"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@drawable/oil_window_shape"
  7. android:gravity="center_horizontal"
  8. android:orientation="vertical" >
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center"
  13. android:padding="10dip"
  14. android:text="今日油价"
  15. android:textColor="@color/white"
  16. android:textSize="@dimen/text_normal_size" />
  17. <View
  18. android:layout_width="match_parent"
  19. android:layout_height="1dip"
  20. android:layout_margin="2dip"
  21. android:background="@color/line_gray" />
  22. <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  23. android:id="@+id/oil_90_layout"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_marginLeft="2dip"
  27. android:layout_marginRight="2dip"
  28. android:background="@color/red"
  29. custom:leftText="90#汽油"
  30. custom:leftTextColor="@color/white"
  31. custom:leftTextSize="@dimen/text_small_size"
  32. custom:rightText="¥6.98"
  33. custom:rightTextColor="@color/white"
  34. custom:rightTextSize="@dimen/text_small_size" />
  35. <View
  36. android:layout_width="match_parent"
  37. android:layout_height="1dip"
  38. android:layout_margin="2dip"
  39. android:background="@color/line_gray" />
  40. <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  41. android:id="@+id/oil_93_layout"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:layout_marginLeft="2dip"
  45. android:layout_marginRight="2dip"
  46. custom:leftText="93#汽油"
  47. custom:leftTextColor="@color/white"
  48. custom:leftTextSize="@dimen/text_small_size"
  49. custom:rightText="¥5.92"
  50. custom:rightTextColor="@color/white"
  51. custom:rightTextSize="@dimen/text_small_size" />
  52. <View
  53. android:layout_width="match_parent"
  54. android:layout_height="1dip"
  55. android:layout_margin="2dip"
  56. android:background="@color/line_gray" />
  57. <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  58. android:id="@+id/oil_97_layout"
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:layout_marginLeft="2dip"
  62. android:layout_marginRight="2dip"
  63. custom:leftText="97#汽油"
  64. custom:leftTextColor="@color/white"
  65. custom:leftTextSize="@dimen/text_small_size"
  66. custom:rightText="¥7.62"
  67. custom:rightTextColor="@color/white"
  68. custom:rightTextSize="@dimen/text_small_size" />
  69. <View
  70. android:layout_width="match_parent"
  71. android:layout_height="1dip"
  72. android:layout_margin="2dip"
  73. android:background="@color/line_gray" />
  74. <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  75. android:id="@+id/oil_0_layout"
  76. android:layout_width="match_parent"
  77. android:layout_height="wrap_content"
  78. android:layout_marginLeft="2dip"
  79. android:layout_marginRight="2dip"
  80. custom:leftText="柴油"
  81. custom:leftTextColor="@color/white"
  82. custom:leftTextSize="@dimen/text_small_size"
  83. custom:rightText="¥6.28"
  84. custom:rightTextColor="@color/white"
  85. custom:rightTextSize="@dimen/text_small_size" />
  86. <View
  87. android:layout_width="match_parent"
  88. android:layout_height="1dip"
  89. android:layout_margin="2dip"
  90. android:background="@color/line_gray" />
  91. </LinearLayout>
很简单的垂直线性布局,具体效果看那个红色的窗口就知道了。

然后就是控制一下PopupWIndow在界面的什么位置显示了。
这里是写在OnclickListener中的
   
  1. case R.id.main_tab02:
  2. /*
  3. * getIntentTool().intent_to(Phone_MainActivity.this,
  4. * Mix_RankList_Activity.class);
  5. */
  6. if (pop != null && pop.isShowing()) {
  7. pop.dismiss();
  8. } else if (pop != null) {
  9. pop.showAtLocation(arg0, Gravity.BOTTOM, 0, 0);// popupWindow居中显示
  10. } else if (pop == null) {
  11. ToastUtil.WarnImageToast(Phone_MainActivity.this, "油价获取中",
  12. "short");
  13. }
  14. break;
判断一下窗口是否初始化成功,并且判断窗口是否在显示中,这里要注意以下,为了让窗口显示在界面底部,代码需要这样写:
   
  1. pop.showAtLocation(arg0, Gravity.BOTTOM, 0, 0);// popupWindow底部显示
  2. pop.showAtLocation(arg0, Gravity.CENTER, 0, 0);// popupWindow底部显示
这样一个基本的PopupWindow基本上就做好了
但是还是没有动画效果
如何加如动画效果呢?
这里要用到Style和属性动画的概念了:

进入屏幕动画:popupwindow_in.xml
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:fromXDelta="0"
  5. android:toXDelta="0"
  6. android:fromYDelta="3000"
  7. android:toYDelta="0"
  8. android:duration="500" />
  9. </set>
500毫秒内,从组件位于屏幕的Y轴3000的方向向屏幕的0点方向移动
离开屏幕动画就是把进入动画反过来就好了:
popupwindow_out.xml
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:fromXDelta="0"
  5. android:toXDelta="0"
  6. android:fromYDelta="0"
  7. android:toYDelta="3000"
  8. android:duration="500" />
  9. </set>
然后在Style中定义一下这个动画切换效果:
   
  1. <style name="popwin_anim_style">
  2. <item name="android:windowEnterAnimation">@anim/popupwindow_in</item>
  3. <item name="android:windowExitAnimation">@anim/popupwindow_out</item>
  4. </style>
这样数据就可以显示了
最后在PopupWindow中初始化调用时设置一下就好了,具体参见方法: initPopupWindow中的一代码:
   
  1. pop.setAnimationStyle(R.style.popwin_anim_style);
至此,一个带动画的PopupWindow就写完了,整体的逻辑思路是这样的:

1.先定义布局界面,确定Window中要显示什么。
2.再初始化PopupWindow,确定其显示的相关参数,并将自定义的View与PopupWindow进行绑定。
3.确定Window的触发条件,并确定窗口弹出位置
4.在anim中加入动画效果,并在Style中定义改动画效果。
5.随后在初始化中利用setAnimationStyle方法设置动画。




来自为知笔记(Wiz)


转载于:https://my.oschina.net/t5xgkow/blog/510476

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

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

相关文章

php 图片无法删除,php如何删除上传的图片

php删除上传的图片的方法&#xff1a;首先检查上传文件是否在允许上传的类型&#xff1b;然后获取图片的完整路径&#xff1b;最后通过“unlink(“uppic/”.$img);”方法删除图片即可。简单的PHP上传图片和删除图片示例代码分享一例简单的PHP上传图片和删除图片示例代码&#x…

贝克汉姆-囚

转载于:https://www.cnblogs.com/andyxl/p/4215954.html

Androida规划nt打包

1.准备工作 &#xff08;1&#xff09;首先安装好ant工具 &#xff08;2&#xff09;生成keystore 在jdk的bin文件夹下 输入keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore 按操作输入就可以&#xff0c;记住password。 &am…

[树结构]平衡二叉树AVL

平衡二叉树是一种二叉排序树&#xff0c;其中每一个节点的左子树和右子树的高度至多等于1&#xff0c;平衡二叉树又称为AVL树。 将二叉树节点的左子树深度减去右子树深度的值称为平衡因子BF&#xff0c;平衡二叉树上所有节点的平衡因子只可能是-1,0或者1。 距离插入点最近的&am…

发布《Linux工具快速教程》

发布《Linux工具快速教程》 阶段性的完成了这本书开源书籍&#xff0c;发布出来给有需要的朋友&#xff0c;同时也欢迎更多的朋友加入进来&#xff0c;完善这本书&#xff1b; 本书Github地址&#xff1a;https://github.com/me115/linuxtools_rst 在线阅读 缘起 Linux下有很多…

linux 部署php svn,Linux服务器搭建svn环境方法详解

下面由Linux教程栏目给大家介绍Linux服务器搭建svn环境的方法&#xff0c;希望对需要的朋友也是帮助&#xff01;1、安装svn服务端sudo apt-get install subversion2、安装svn在ubuntu的本地客户端sudo apt-get install libapache2-svn3、在根目录home下面建一个文件夹svn&…

pfsense 2.2RC版本应用

为什么要上RC版本呢&#xff1f;因为华硕主板有一个RTL8111G驱动在2.15中还没有识别。。。。 公司双线WAN&#xff0c;一个PPPOE一个静态IP。 开了端口转发&#xff0c; 要求对不同的IP进行相关限速&#xff0c; 到达指定网站用固定IP&#xff0c; 两根线带宽均衡使用。 相关设…

我的Android进阶之旅------Android利用温度传感器实现带动画效果的电子温度计

要想实现带动画效果的电子温度计&#xff0c;需要以下几个知识点&#xff1a;1、温度传感器相关知识。2、ScaleAnimation动画相关知识&#xff0c;来进行水印刻度的缩放效果。3、android:layout_weight属性的合理运用&#xff0c;关于android:layout_weight属性的讲解&#xff…

mybatis中的#和$的区别

为什么80%的码农都做不了架构师&#xff1f;>>> 1. #将传入的数据都当成一个字符串&#xff0c;会对自动传入的数据加一个双引号。如&#xff1a;order by #user_id#&#xff0c;如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是…

MINA2 源代码学习--源代码结构梳理

一、mina总体框架与案例&#xff1a; 1.总体结构图&#xff1a; 简述&#xff1a;以上是一张来自网上比較经典的图&#xff0c;总体上揭示了mina的结构&#xff0c;当中IoService包括clientIoConnector和服务端IoAcceptor两部分。即不管是client还是服务端都是这个结构。IoServ…

如何查看mac系统是32位还是64位的操作系统

&#xff08;一&#xff09;点击工具栏左上角点击 &#xff08;苹果Logo&#xff09;标志&#xff0c;关于本机 --> 更多信息 --> 系统报告 -->(左侧栏中)软件 &#xff08;二&#xff09;打开终端&#xff0c;输入命令 uname -a 回车 x86_64 表示系统为64位 i68…

3ds max删除了对象后,还是将原来所有对象输出的原因

原因是场景中除了 几何体 外还有 图形&#xff0c;如下图 将这些图形删除&#xff0c;几何体就都正常输出了。 转载于:https://www.cnblogs.com/qingsunny/p/4236530.html

后代选择器

1 <!DOCTYPE HTML>2 <html>3 <head>4 <meta http-equiv"Content-Type" content"text/html; charsetutf-8">5 <title>后代选择器</title>6 <style type"text/css">7 .first span{8 color:red;9 …

u-boot分析(八)----串口初始化

u-boot分析&#xff08;八&#xff09; 上篇博文我们按照210的启动流程&#xff0c;分析到了内存初始化&#xff0c;今天我们继续按照u-boot的启动流程对串口的初始化进行分析。 今天我们会用到的文档&#xff1a; 1. 2440芯片手册&#xff1a;http://download.csdn.net…

linux 开启防火墙的指定端口

2019独角兽企业重金招聘Python工程师标准>>> 通过下面的命令可以开启允许对外访问的网络端口&#xff1a; /sbin/iptables -I INPUT -p tcp --dport 8011 -j ACCEPT #开启8011端口 /etc/rc.d/init.d/iptables save #保存配置 /etc/rc.d/init.d/iptables restart #…

关于webstorm 配置 banbel

2019独角兽企业重金招聘Python工程师标准>>> file type : javascript files scope: project files program: ..............appData\Roaming\npm\babel.cmd argumemt: $FileName$ --out-file $FileNameWithoutExtension$.js -s ps: --out-put 输出es5 格式的文件 …

微信朋友圈广告详细说明

根据官方发布的微信广告系统介绍&#xff0c;朋友圈广告来源于微信广告的一部分&#xff0c;与公众号广告形成了一整个体系。关于公众号的广告&#xff0c;简单说&#xff0c;他可以把微信公众号变成广告牌&#xff0c;会根据用户的阅读习惯以及个人信息来进行广告的投放。每一…

EasyExcel 导出文件的格式化

阿里开源的这个库&#xff0c;让 Excel 导出不再复杂&#xff08;既要能写&#xff0c;还要写的好看&#xff09; 之前聊了 EasyExcel 的内容导出&#xff0c;本文主要说一下导出文件的格式化&#xff0c;格式化包括工作表/单元格样式和内容格式化。毕竟&#xff0c;有时候还是…

oracle闪回某个时间点的数据库,oracle11g 使用闪回查询恢复表到过去某一个时间点...

一、新建测试表并插入数据&#xff1a;oracle二、模拟表数据误删并提交&#xff1a;测试三、使用闪回查询来查误删前表的数据&#xff1a;(表误删是在15:08分左右误删&#xff0c;所以在15:08分以前表的数据仍是在的)&#xff1a;spa四、用如今的数据与误删前的数据作对比&…

opengl微开发之1-从零開始

对OpenGL有一点了解之后&#xff0c;如今開始真正编写代码。 今天的内容&#xff1a; 使用FreeGLUT创建OpenGL的上下文环境 初始化GLEW 创建一个OpenGL的的模板范例 第一步&#xff1a; 一个OpenGL的上下文能够同意我们传递命令究竟层硬件&#xff0c;所以须要一个上下文环境。…