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)

以下是他回忆当时的笔试题一、1、有如下HTML&#xff1a;1)用js取得________方法取得该对象&#xff1b;2)用________属性取得属性title的属性值&#xff1b;3)用________方法取得属性sina_title的属性值&#xff1b;2、php中对数组序列化和反序列化的函数分别是______和______…

Nodejs从小工到专家系列(一)

前言 从小工到专家系列为从头开始学习Nodejs,但它并不是教程,我会整理一些应该特别注意或深入理解的知识点,当然也会涉及常用的库,比如Express,mongoose,bluebird.. Nodejs特点: 单线程 优点: 没有死锁存在没有线程上下文交换所产生的性能开销缺点: 无法利用多核错误会引起整个…

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

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

textbox 和textera 文本框多行后不能拉伸

加一个样式 "style "resize:none"转载于:https://www.cnblogs.com/q101301/p/4213298.html

Linux中断(interrupt)子系统之五:软件中断(softIRQ)

转自&#xff1a;http://blog.csdn.net/droidphone/article/details/7518428 软件中断&#xff08;softIRQ&#xff09;是内核提供的一种延迟执行机制&#xff0c;它完全由软件触发&#xff0c;虽然说是延迟机制&#xff0c;实际上&#xff0c;在大多数情况下&#xff0c;它与普…

php 克隆对象,php中对象的复制与克隆

* 对象的复制与克隆* 1.默认情况下,对象是引用传递(实际上是对象标识符的复制,后面会详细说)* 2.也就是说二个对象变量实际上是引用的是同一个对象* 3.如果要创建一个新的对象,必须使用clone关键字来克隆当前对象* 4.当使用clone关键字时,如果类中有__clone()会自动调用* 5.__c…

贝克汉姆-囚

转载于: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…

php5.3+for+linux,Centos 安装 nginx + php5.3

Centos 安装 nginx php5.3&#xff0c;点开查看详情。 #查看系统版本信息cat /etc/issue uname -a#设置时区 rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime#使用ntpdate同步时间 yum install -y ntp ntpdate -u pool.ntp.org date#Centos 安…

PHP Uploadify+jQuery.imgAreaSelect插件+AJAX 实现图片上传裁剪 仿微博头像上传功能

http://blog.csdn.net/as66t/article/details/11688217 http://blog.mc-zone.me/article/226#comment-2991转载于:https://www.cnblogs.com/ymj0906/p/4221967.html

[树结构]平衡二叉树AVL

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

dedecms php5.4 无法退出后台,PHP5.4版本织梦dedecms后台退出空白的解决方法

你是否遇到过PHP5.4版本织梦dedecms后台退出空白的问题&#xff0c;有没有解决呢?没有解决思绪&#xff0c;就来看看我这篇文章吧。解决办法&#xff1a;打开include/userlogin.class.php找到&#xff1a;function exitUser(){ClearMyAddon();session_unregister($this->ke…

C++构造函数/析构函数 设置成private的原因

C构造函数/析构函数 设置成private的原因 标签&#xff08;空格分隔&#xff09;&#xff1a; c/c 将构造函数&#xff0c;析构函数声明为私有和保护的&#xff0c;那么对象如何创建&#xff1f; 已经不能从外部调用构造函数了&#xff0c;但是对象必须被构造&#xff0c;应该如…

R语言学习笔记(4)

第四章&#xff1a;基本数据管理 一 贯穿整章的示例 二 变量的创建、重编码和重命名 三 日期值与缺失值 四 数据类型和类型转换 五 数据集的排序、合并与取子集 一 贯穿整章的示例&#xff08;leadership&#xff09; 代码4-1 1 > manager<-c(1,2,3,4,5)2 > date<…

php substr_replace 中文乱码,php substr_replace替换字符串一些实例_PHP教程

substr_replace与str_replace有一点像就是直接把字符串替换一部份了&#xff0c;下面小编来给各位同学介绍一下操作方法。substr_replace() 函数把字符串的一部分替换为另一个字符串。用法substr_replace(string,replacement,start,length)注意当字符串包含中文时&#xff0c;不…

发布《Linux工具快速教程》

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

java6:流程控制

Java 流程控制&#xff1a;顺序分支循环分支&#xff1a;if(布尔表达式){语句块}else{语句块}尽量使用肯定条件&#xff0c;减少else&#xff0c;减少嵌套package day06; import java.util.Scanner; public class Demo01 {public static void main(String[] args) {Scanner con…

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…