方法一的Activity

  1. package com.app.test02;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.View.OnTouchListener;
  9. import android.view.ViewGroup.LayoutParams;
  10. import android.widget.Button;
  11. import android.widget.PopupWindow;
  12. import android.widget.Toast;
  13. public class PopwindowLeft extends Activity {
  14. // 声明PopupWindow对象的引用
  15. private PopupWindow popupWindow;
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_popupwindow_main);
  21. // 点击按钮弹出菜单
  22. Button pop = (Button) findViewById(R.id.popBtn);
  23. pop.setOnClickListener(popClick);
  24. }
  25. // 点击弹出左侧菜单的显示方式
  26. OnClickListener popClick = new OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. // TODO Auto-generated method stub
  30. getPopupWindow();
  31. // 这里是位置显示方式,在屏幕的左侧
  32. popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
  33. }
  34. };
  35. /**
  36. * 创建PopupWindow
  37. */
  38. protected void initPopuptWindow() {
  39. // TODO Auto-generated method stub
  40. // 获取自定义布局文件activity_popupwindow_left.xml的视图
  41. View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,
  42. false);
  43. // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
  44. popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
  45. // 设置动画效果
  46. popupWindow.setAnimationStyle(R.style.AnimationFade);
  47. // 点击其他地方消失
  48. popupWindow_view.setOnTouchListener(new OnTouchListener() {
  49. @Override
  50. public boolean onTouch(View v, MotionEvent event) {
  51. // TODO Auto-generated method stub
  52. if (popupWindow != null && popupWindow.isShowing()) {
  53. popupWindow.dismiss();
  54. popupWindow = null;
  55. }
  56. return false;
  57. }
  58. });
  59. }
  60. /***
  61. * 获取PopupWindow实例
  62. */
  63. private void getPopupWindow() {
  64. if (null != popupWindow) {
  65. popupWindow.dismiss();
  66. return;
  67. } else {
  68. initPopuptWindow();
  69. }
  70. }
  71. }

方法二的Activity

  1. package com.app.test02;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.View.OnTouchListener;
  9. import android.view.ViewGroup.LayoutParams;
  10. import android.widget.PopupWindow;
  11. public class PopwindowLeftNew extends Activity{
  12. private PopupWindow popupWindow;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. // TODO Auto-generated method stub
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_popupwindow_main);
  18. findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. // TODO Auto-generated method stub
  22. // 获取自定义布局文件activity_popupwindow_left.xml的视图
  23. View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);
  24. // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
  25. popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
  26. // 设置动画效果
  27. popupWindow.setAnimationStyle(R.style.AnimationFade);
  28. // 这里是位置显示方式,在屏幕的左侧
  29. popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
  30. // 点击其他地方消失
  31. popupWindow_view.setOnTouchListener(new OnTouchListener() {
  32. @Override
  33. public boolean onTouch(View v, MotionEvent event) {
  34. // TODO Auto-generated method stub
  35. if (popupWindow != null && popupWindow.isShowing()) {
  36. popupWindow.dismiss();
  37. popupWindow = null;
  38. }
  39. return false;
  40. }
  41. });
  42. }
  43. });
  44. }
  45. }

效果图

 
 

附:一些相关的布局文件

PopupWindow弹出菜单

activity_popupwindow_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:background="#fff" >
  7. <Button android:id="@+id/popBtn"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="弹出左侧菜单" />
  11. </LinearLayout>

activity_popupwindow_left.xml

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:background="@android:color/darker_gray"
  5. android:orientation="vertical"
  6. android:gravity="center"
  7. android:paddingTop="50dp">
  8. <Button
  9. android:id="@+id/open"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:background="@android:color/darker_gray"
  14. android:text="打开" />
  15. <Button
  16. android:id="@+id/save"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="1"
  20. android:background="@android:color/darker_gray"
  21. android:text="保存" />
  22. <Button
  23. android:id="@+id/close"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:background="@android:color/darker_gray"
  28. android:text="关闭" />
  29. <Button
  30. android:id="@+id/open"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:background="@android:color/darker_gray"
  35. android:text="打开" />
  36. <Button
  37. android:id="@+id/save"
  38. android:layout_width="fill_parent"
  39. android:layout_height="wrap_content"
  40. android:layout_weight="1"
  41. android:background="@android:color/darker_gray"
  42. android:text="保存" />
  43. <Button
  44. android:id="@+id/close"
  45. android:layout_width="fill_parent"
  46. android:layout_height="wrap_content"
  47. android:layout_weight="1"
  48. android:background="@android:color/darker_gray"
  49. android:text="关闭" />
  50. <Button
  51. android:id="@+id/open"
  52. android:layout_width="fill_parent"
  53. android:layout_height="wrap_content"
  54. android:layout_weight="1"
  55. android:background="@android:color/darker_gray"
  56. android:text="打开" />
  57. <Button
  58. android:id="@+id/save"
  59. android:layout_width="fill_parent"
  60. android:layout_height="wrap_content"
  61. android:layout_weight="1"
  62. android:background="@android:color/darker_gray"
  63. android:text="保存" />
  64. <Button
  65. android:id="@+id/close"
  66. android:layout_width="fill_parent"
  67. android:layout_height="wrap_content"
  68. android:layout_weight="1"
  69. android:background="@android:color/darker_gray"
  70. android:text="关闭" />
  71. </LinearLayout>

弹出动画XML

在res文件夹下,建立anim文件夹。写入如下两个文件。
弹出动画
in_lefttoright.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- 定义从左向右进入的动画 -->
  4. <translate
  5. android:duration="500"
  6. android:fromXDelta="-100%"
  7. android:toXDelta="0" />
  8. </set>
 
弹回动画
out_righttoleft.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- 定义从右向左动画退出动画 -->
  4. <translate
  5. android:duration="500"
  6. android:fromXDelta="0"
  7. android:toXDelta="-100%" />
  8. </set>

动画管理

在styles.xml中,添加如下管理代码。
  1. <style name="AnimationFade">
  2. <!-- PopupWindow左右弹出的效果 -->
  3. <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
  4. <item name="android:windowExitAnimation">@anim/out_righttoleft</item>
  5. </style>

【转】android创建Popwindow弹出菜单的两种方式的更多相关文章

  1. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  2. JS弹出对话框的三种方式

    JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...

  3. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  4. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  5. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  6. Android中H5和Native交互的两种方式

    Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...

  7. android 长按弹出菜单,复制,粘贴,全选

    <!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  8. js弹出对话框的三种方式(转)

    原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...

  9. Android更改桌面应用程序launcher的两种方式

    http://blog.csdn.net/mdx20072419/article/details/9632779/ launcher,也就是android的桌面应用程序.下图是我正在使用的魅族手机的l ...

随机推荐

  1. Round #169 (Div. 2)D. Little Girl and Maximum XOR

    1.首先是要找到一个位置从左至右,作l这一个是0,r这一个是1. 2.实例01011,10100.你将能够找到01111和10000. #include<cstdio> #include& ...

  2. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. CSS3图片轮播效果

    原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且 ...

  4. 华为-on练习--重复的字符过滤

    称号: 请写一个字符串过滤程序,如果使用多个相同的字符出现在字符串中,字符首次出现在非过滤,. 比方字符串"abacacde"过滤结果为"abcde". 演示样 ...

  5. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

  6. RPM安装包-Spec文件參数具体解释与演示样例分析

    spec文件是整个RPM包建立过程的中心,它的作用就如同编译程序时的Makefile文件. 1.Spec文件參数 spec文件包括建立一个RPM包必需的信息,包括哪些文件是包的一部分以及它们安装在哪个 ...

  7. 基于PHP的crontab定时任务管理

    BY JENNER · 2014年11月10日· 阅读次数:6 linux的crontab一直是server运维.业务开展的利器.但当定时任务增多时,管理和迁移都变得非常麻烦,并且easy出问题.以下 ...

  8. 为什么数据线easy糟糕

    一个好的设计可以帮助解决问题似乎无关紧要豪. 两天前M3数据线被破坏.在弯附近的电话插口可以充电.一松就充不了电了. 今天突然想到每次充电的时候用手机发信息.玩游戏都特别不方便,才想到为什么数据线ea ...

  9. hdu 1251(字典树)

    题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5 Trie树的基本实现 字母树的插入(Inse ...

  10. 解决tomcat占用8080端口

    怎么解决tomcat占用8080端口问题图文教程           怎么解决tomcat占用8080端口问题 相信很多朋友都遇到过这样的问题吧,tomcat死机了,重启eclipse之后,发现 Se ...