方法一的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. Android Context MODE的四种模式解析(转)

    Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中.可以使用Context ...

  2. (大数据工程师学习路径)第三步 Git Community Book----Git基本用法(上)

    一.git的初始化 1.Git 配置 使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名. $ git config --global user.name &quo ...

  3. iOS:删除小程序

    //Applet的批次从父视图中移除 NSArray *subViews = [_scrollView subviews]; if([subViews count] != 0) { [subViews ...

  4. Android 定义自己的学习(5)它们的定义Progressbar

    它们的定义View最后的研究,首先在效果图. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDE2MzcyNg==/font/5a6L5L2T/f ...

  5. mysql 在创建批处理脚本日志表信息

    mysql在批处理脚本通过存储过程如下所示创建日志信息表: drop PROCEDURE if EXISTS reqSp; DELIMITER // create procedure reqSp(sT ...

  6. react学习笔记1--基础知识

    什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...

  7. 我的Linux学习历程:那些我看过的Linux书籍们

    [+]查看原图http://www.ituring.com.cn/article/119401 来北京工作已经一个多月,大都市的生活比起读大学要忙碌得多,尤其是出行,基本以小时为基本的计时单位.有时茫 ...

  8. Ubuntu通过使用PyCharm 进行调试 Odoo 8.0 可能出现的问题

    实现步骤,请移步http://shine-it.net/index.php?topic=16603.0 要么 http://www.mindissoftware.com/2014/09/11/Run- ...

  9. crawler_分布式网络爬虫的设计与实现_设计图

    一.集中调度式 二.p2p 三.混合调度式 四.大型集群

  10. node.js的npm安装

    我不打算引进node.js的npm安装,但发现node.js通过管理一些包npm实现,或给一个简短的npm. 1.npm什么        npm是一个node包管理和分发工具,已经成为了非官方的公布 ...