方法一的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. 更多RANK37

    By LTL 经过对BDG长期的观察得出的这套理论,希望能对大家学习OI有所帮助. 入门篇 首先在掌握一门语言时(P,C都无所谓的),假设不习惯指针能够直接无视.语言之中对于入门选手来说最重要的递归. ...

  2. c++ primer plus(文章6版本)中国版 编程练习答案第八章

    编程练习答案第八章 8.1写输出字符串的函数,存在默认参数表示输出频率,莫感觉1.(原标题太扯了,的问题的细微变化的基础上,含义) //8.1编写一个输出字符串的函数.有一个默认參数表示输出次数,默觉 ...

  3. poj 1160 Post Office (间隔DP)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Desc ...

  4. 十天学Linux内核之第一天---内核探索工具类

    原文:十天学Linux内核之第一天---内核探索工具类 寒假闲下来了,可以尽情的做自己喜欢的事情,专心待在实验室里燥起来了,因为大二的时候接触过Linux,只是关于内核方面确实是不好懂,所以十天的时间 ...

  5. ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开篇

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开篇 前言:博客又有一段时间没有更新了,心里感觉这段时间空空的,好像什么都没有学下,所以就想写博客,所以就有了这个系列,这里当然也 ...

  6. Android研究之游戏开发处理按键的响应

    1.onKeyDown 方法 onKeyDown 方法是KeyEvent.Callback 接口中的一个抽象方法,重写onKeyDown 方法能够监听到按键被按下的事件,我们先看看onKeyDown方 ...

  7. django中通过model名字获取model

    django1.6, 通过字符串和get_app.get_model获得对应的object 只需要两行代码: from django.db.models import get_model get_mo ...

  8. CSS3+HTML5特效7 - 特殊的 Loading 效果

    效果如下     实现原理: 利用CSS3的@keyframes规则创建动画效果: 使用CSS3的animation效果完成滚动切换. 代码说明: 样式表中@-webkit-keyframes及@ke ...

  9. Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串

    原文:Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串 声明xml字符串: string xml = ...

  10. new 和delete

    转自:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1899982.html 一直对C++中的delete和delete[]的区别不甚了 ...