方法一的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. ubuntu经常使用的命令摘要

    1.df命令 # df -ha 显示所有文件和分区的使用 # df -h /dev/sda1 显示sda1磁盘使用率 # df -T 显示文件系统名称属于每个分区.区的格式类型(比方ext3) 注:h ...

  2. The JSON request was too large to be deserialized

    The JSON request was too large to be deserialized 这个问题出现的场景并不是很多,当你向服务端异步(ajax)post数据非常大的情况下(比如做权限管理 ...

  3. linux下一个Oracle11g RAC建立(四)

    linux下一个Oracle11g RAC建立(四) 三.配置共享存储 配置ASM管理准备 1)OCRDISK :存储CRS资源配置信息 2)VOTEDISK:仲裁盘.记录节点状态 3)DataDis ...

  4. about greenplum collection tool

    three collection tool for greenplum:pstack.strace.gcore.                                            ...

  5. 看德日进,凯文·凯利与Kurzweil老师?

    生命从哪里来.要到那里去.生命存在的意义是什么.这些差点儿是人类可以探究的最深层次问题.基督教给出的答案是毁灭和审判.佛学给出的答案是无常,科学的达尔文进化论给出了生命的起点和进化的过程,对于未来.达 ...

  6. 解决Gradle minifyEnabled无法找到错误

    Android studio最新版0.9支持proguard minify,shrinkResources ,尝试了下,提示找不到minifyEnabled方法 Error:(29, 0) Gradl ...

  7. HDOJ 3037 Saving Beans

    如果您有n+1树,文章n+1埋不足一棵树m种子,法国隔C[n+m][m] 大量的组合,以取mod使用Lucas定理: Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/ ...

  8. Node.js v0.10.31API手工-DNS

    原版的API品种,这是从以前的翻译和翻译风格不同 Node.js v0.10.31API手冊-文件夹 DNS 使用 require('dns') 引入此模块. dns 模块中的全部方法都使用了 C-A ...

  9. android_Activity生命周期功能

    说明:初步activity 生命周期7功能. 样本:于MainActivity我加了button,搬家button.跳到OtherActivity.控制台输出的观察. 让我们来看看这些功能: 他们的流 ...

  10. C#关于图片的相关处理

    public class ImageHelper { /// <summary> /// 图片转换成字节流 /// </summary> /// <param name= ...