方法一的Activity

[java] view plain copy
  1. packagecom.app.test02;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.Gravity;
  5. importandroid.view.MotionEvent;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.view.View.OnTouchListener;
  9. importandroid.view.ViewGroup.LayoutParams;
  10. importandroid.widget.Button;
  11. importandroid.widget.PopupWindow;
  12. importandroid.widget.Toast;
  13. publicclassPopwindowLeftextendsActivity{
  14. //声明PopupWindow对象的引用
  15. privatePopupWindowpopupWindow;
  16. /**Calledwhentheactivityisfirstcreated.*/
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_popupwindow_main);
  21. //点击按钮弹出菜单
  22. Buttonpop=(Button)findViewById(R.id.popBtn);
  23. pop.setOnClickListener(popClick);
  24. }
  25. //点击弹出左侧菜单的显示方式
  26. OnClickListenerpopClick=newOnClickListener(){
  27. @Override
  28. publicvoidonClick(Viewv){
  29. //TODOAuto-generatedmethodstub
  30. getPopupWindow();
  31. //这里是位置显示方式,在屏幕的左侧
  32. popupWindow.showAtLocation(v,Gravity.LEFT,0,0);
  33. }
  34. };
  35. /**
  36. *创建PopupWindow
  37. */
  38. protectedvoidinitPopuptWindow(){
  39. //TODOAuto-generatedmethodstub
  40. //获取自定义布局文件activity_popupwindow_left.xml的视图
  41. ViewpopupWindow_view=getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,
  42. false);
  43. //创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
  44. popupWindow=newPopupWindow(popupWindow_view,200,LayoutParams.MATCH_PARENT,true);
  45. //设置动画效果
  46. popupWindow.setAnimationStyle(R.style.AnimationFade);
  47. //点击其他地方消失
  48. popupWindow_view.setOnTouchListener(newOnTouchListener(){
  49. @Override
  50. publicbooleanonTouch(Viewv,MotionEventevent){
  51. //TODOAuto-generatedmethodstub
  52. if(popupWindow!=null&&popupWindow.isShowing()){
  53. popupWindow.dismiss();
  54. popupWindow=null;
  55. }
  56. returnfalse;
  57. }
  58. });
  59. }
  60. /***
  61. *获取PopupWindow实例
  62. */
  63. privatevoidgetPopupWindow(){
  64. if(null!=popupWindow){
  65. popupWindow.dismiss();
  66. return;
  67. }else{
  68. initPopuptWindow();
  69. }
  70. }
  71. }

方法二的Activity

[java] view plain copy
  1. packagecom.app.test02;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.Gravity;
  5. importandroid.view.MotionEvent;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.view.View.OnTouchListener;
  9. importandroid.view.ViewGroup.LayoutParams;
  10. importandroid.widget.PopupWindow;
  11. publicclassPopwindowLeftNewextendsActivity{
  12. privatePopupWindowpopupWindow;
  13. @Override
  14. protectedvoidonCreate(BundlesavedInstanceState){
  15. //TODOAuto-generatedmethodstub
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_popupwindow_main);
  18. findViewById(R.id.popBtn).setOnClickListener(newOnClickListener(){
  19. @Override
  20. publicvoidonClick(Viewv){
  21. //TODOAuto-generatedmethodstub
  22. //获取自定义布局文件activity_popupwindow_left.xml的视图
  23. ViewpopupWindow_view=getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,false);
  24. //创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
  25. popupWindow=newPopupWindow(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(newOnTouchListener(){
  32. @Override
  33. publicbooleanonTouch(Viewv,MotionEventevent){
  34. //TODOAuto-generatedmethodstub
  35. if(popupWindow!=null&&popupWindow.isShowing()){
  36. popupWindow.dismiss();
  37. popupWindow=null;
  38. }
  39. returnfalse;
  40. }
  41. });
  42. }
  43. });
  44. }
  45. }

效果图




附:一些相关的布局文件

PopupWindow弹出菜单

activity_popupwindow_main.xml
[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <Buttonandroid:id="@+id/popBtn"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="弹出左侧菜单"/>
  11. </LinearLayout>

activity_popupwindow_left.xml
[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns: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
[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <setxmlns: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
[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <setxmlns: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中,添加如下管理代码。 [html] view plain copy
  1. <stylename="AnimationFade">
  2. <!--PopupWindow左右弹出的效果-->
  3. <itemname="android:windowEnterAnimation">@anim/in_lefttoright</item>
  4. <itemname="android:windowExitAnimation">@anim/out_righttoleft</item>

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 读取android手机流量信息
  4. Android(安卓)Activity界面切换添加动画特效
  5. android 使用html5作布局文件: webview跟javascript交互
  6. android实现字体闪烁动画的方法
  7. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  8. android“设置”里的版本号
  9. Android开发环境搭建

随机推荐

  1. 交互式实例
  2. Gradle version 与 Android(安卓)Plugin
  3. Volley的四大请求
  4. Android(安卓)xml Activity进入或退出动
  5. 从TikTok事件,聊聊如何用内容煽动情绪
  6. recycleview二级菜单列表
  7. com.android.build.api.transform.Transf
  8. 自定义开关按钮
  9. Android(安卓)自动向手机添加联系人(测试
  10. Activity的AsyncTask请求