【Android】创建Popwindow弹出菜单的两种方式

来源:互联网 发布:nginx查看端口是否 编辑:程序博客网 时间:2024/05/02 01:56

方法一的Activity

[java] view plaincopy
  1. package com.app.test02;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.Button;  
  12. import android.widget.PopupWindow;  
  13. import android.widget.Toast;  
  14.   
  15. public class PopwindowLeft extends Activity {  
  16.     // 声明PopupWindow对象的引用  
  17.     private PopupWindow popupWindow;  
  18.   
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_popupwindow_main);  
  24.         // 点击按钮弹出菜单  
  25.         Button pop = (Button) findViewById(R.id.popBtn);  
  26.         pop.setOnClickListener(popClick);  
  27.     }  
  28.   
  29.     // 点击弹出左侧菜单的显示方式  
  30.     OnClickListener popClick = new OnClickListener() {  
  31.         @Override  
  32.         public void onClick(View v) {  
  33.             // TODO Auto-generated method stub  
  34.             getPopupWindow();  
  35.             // 这里是位置显示方式,在屏幕的左侧  
  36.             popupWindow.showAtLocation(v, Gravity.LEFT, 00);  
  37.         }  
  38.     };  
  39.   
  40.     /** 
  41.      * 创建PopupWindow 
  42.      */  
  43.     protected void initPopuptWindow() {  
  44.         // TODO Auto-generated method stub  
  45.         // 获取自定义布局文件activity_popupwindow_left.xml的视图  
  46.         View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,  
  47.                 false);  
  48.         // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度  
  49.         popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);  
  50.         // 设置动画效果  
  51.         popupWindow.setAnimationStyle(R.style.AnimationFade);  
  52.         // 点击其他地方消失  
  53.         popupWindow_view.setOnTouchListener(new OnTouchListener() {  
  54.             @Override  
  55.             public boolean onTouch(View v, MotionEvent event) {  
  56.                 // TODO Auto-generated method stub  
  57.                 if (popupWindow != null && popupWindow.isShowing()) {  
  58.                     popupWindow.dismiss();  
  59.                     popupWindow = null;  
  60.                 }  
  61.                 return false;  
  62.             }  
  63.         });  
  64.     }  
  65.     /*** 
  66.      * 获取PopupWindow实例 
  67.      */  
  68.     private void getPopupWindow() {  
  69.         if (null != popupWindow) {  
  70.             popupWindow.dismiss();  
  71.             return;  
  72.         } else {  
  73.             initPopuptWindow();  
  74.         }  
  75.     }  
  76. }  

方法二的Activity

[java] view plaincopy
  1. package com.app.test02;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.PopupWindow;  
  12.   
  13. public class PopwindowLeftNew extends Activity{  
  14.     private PopupWindow popupWindow;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_popupwindow_main);  
  20.           
  21.         findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 // TODO Auto-generated method stub  
  25.                 // 获取自定义布局文件activity_popupwindow_left.xml的视图  
  26.                 View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);  
  27.                 // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度  
  28.                 popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);  
  29.                 // 设置动画效果  
  30.                 popupWindow.setAnimationStyle(R.style.AnimationFade);  
  31.                 // 这里是位置显示方式,在屏幕的左侧  
  32.                 popupWindow.showAtLocation(v, Gravity.LEFT, 00);  
  33.                 // 点击其他地方消失  
  34.                 popupWindow_view.setOnTouchListener(new OnTouchListener() {  
  35.                     @Override  
  36.                     public boolean onTouch(View v, MotionEvent event) {  
  37.                         // TODO Auto-generated method stub  
  38.                         if (popupWindow != null && popupWindow.isShowing()) {  
  39.                             popupWindow.dismiss();  
  40.                             popupWindow = null;  
  41.                         }  
  42.                         return false;  
  43.                     }  
  44.                 });  
  45.             }  
  46.         });  
  47.           
  48.     }  
  49. }  

效果图




附:一些相关的布局文件

PopupWindow弹出菜单

activity_popupwindow_main.xml
[html] view plaincopy
  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.       
  8.     <Button android:id="@+id/popBtn"   
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:text="弹出左侧菜单" />   
  12.           
  13. </LinearLayout>  

activity_popupwindow_left.xml
[html] view plaincopy
  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.   
  9.     <Button  
  10.         android:id="@+id/open"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_weight="1"  
  14.         android:background="@android:color/darker_gray"  
  15.         android:text="打开" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/save"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_weight="1"  
  22.         android:background="@android:color/darker_gray"  
  23.         android:text="保存" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/close"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_weight="1"  
  30.         android:background="@android:color/darker_gray"  
  31.         android:text="关闭" />  
  32.   
  33.   
  34.     <Button  
  35.         android:id="@+id/open"  
  36.         android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_weight="1"  
  39.         android:background="@android:color/darker_gray"  
  40.         android:text="打开" />  
  41.   
  42.     <Button  
  43.         android:id="@+id/save"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:layout_weight="1"  
  47.         android:background="@android:color/darker_gray"  
  48.         android:text="保存" />  
  49.   
  50.     <Button  
  51.         android:id="@+id/close"  
  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.       
  58.     <Button  
  59.         android:id="@+id/open"  
  60.         android:layout_width="fill_parent"  
  61.         android:layout_height="wrap_content"  
  62.         android:layout_weight="1"  
  63.         android:background="@android:color/darker_gray"  
  64.         android:text="打开" />  
  65.   
  66.     <Button  
  67.         android:id="@+id/save"  
  68.         android:layout_width="fill_parent"  
  69.         android:layout_height="wrap_content"  
  70.         android:layout_weight="1"  
  71.         android:background="@android:color/darker_gray"  
  72.         android:text="保存" />  
  73.   
  74.     <Button  
  75.         android:id="@+id/close"  
  76.         android:layout_width="fill_parent"  
  77.         android:layout_height="wrap_content"  
  78.         android:layout_weight="1"  
  79.         android:background="@android:color/darker_gray"  
  80.         android:text="关闭" />  
  81.       
  82. </LinearLayout>  

弹出动画XML

在res文件夹下,建立anim文件夹。写入如下两个文件。
弹出动画
in_lefttoright.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <!-- 定义从左向右进入的动画 -->  
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="-100%"  
  8.         android:toXDelta="0" />  
  9.   
  10. </set>  

弹回动画
out_righttoleft.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <!-- 定义从右向左动画退出动画 -->  
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="0"  
  8.         android:toXDelta="-100%" />  
  9.   
  10. </set>  

动画管理

在styles.xml中,添加如下管理代码。
[html] view plaincopy
  1. <style name="AnimationFade">  
  2.   
  3.     <!-- PopupWindow左右弹出的效果 -->  
  4.     <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>  
  5.     <item name="android:windowExitAnimation">@anim/out_righttoleft</item>  
  6. </style>  
0 0
原创粉丝点击