方法一的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. php形式的内容被处理

    /** * 过滤HTML内容RETURN * * @param $string * @param bool $html * * @return array|string */ public stati ...

  2. Deep Learning Papers

    一.Image Classification(Recognition) lenet: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf alexn ...

  3. Android SDCard Mount 流程分析

    前段时间对Android 的SDCard unmount 流程进行了几篇简短的分析,由于当时只是纸上谈兵,没有实际上的跟进,可能会有一些误导人或者小错误.今天重新梳理了头绪,针对mount的流程再重新 ...

  4. 二叉搜索树(Binary Search Tree)--C语言描述(转)

    图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...

  5. hdu 4864 Task---2014 Multi-University Training Contest 1

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS (Java/Others)    M ...

  6. JavaScript通告/订阅的例子

    原文链接: Pub/Sub JavaScript Object原始日期: 2014年6一个月11日本: 2014年6月13日 翻译人员: 铁锚 高效AJAX站点的三大杀器: 事件代理, 浏览历史管理, ...

  7. js 正则学习小记之匹配字符串优化篇

    原文:js 正则学习小记之匹配字符串优化篇 昨天在<js 正则学习小记之匹配字符串>谈到 个字符,除了第一个 个,只有 个转义( 个字符),所以 次,只有 次成功.这 次匹配失败,需要回溯 ...

  8. 《代码的第一行——Android》封面诞生

    <代码的第一行--Android>已经上市近一个月,现在的情况是相当不错的销售,也特别感谢众多朋友的支持. 其实一本好书,假设你想卖.除了给予外力所要求的内容.封面设计是至关重要的,这本书 ...

  9. oracle_oracle中修改日期的显示格式

    我的现在的日期格式是          ,要改成英文的需要输入一下命令: ALTER SESSION SET NLS_DATE_LANGUAGE=AMERICAN; 修改后变为: 同样也得若是英文要想 ...

  10. 兼容安卓的javaproject1.0

    <pre class="java" name="code"> //兼容安卓的系统package cn.com.likeshow; import ja ...