【Android】使用Popwindow实现左侧弹出菜单层

PopupWindow可以实现浮层效果。

主要方法有:可以自定义view,通过LayoutInflator方法;可以出现和退出时显示动画;可以指定显示位置等。 


为了将PopupWindow的多个功能展现并力求用简单的代码实现,编写了一个点击按钮左侧弹出菜单的功能,实现出现和退出时显示动画效果并点击其他区域时弹出层自动消失。


Activity文件

[java]   view plain copy
  1. package com.app.test02;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  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.widget.Button;  
  10. import android.widget.PopupWindow;  
  11. import android.widget.Toast;  
  12.   
  13. public class PopwindowLeft extends Activity {  
  14.     // 声明PopupWindow对象的引用  
  15.     private PopupWindow popupWindow;  
  16.   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_popupwindow_main);  
  22.         // 点击按钮弹出菜单  
  23.         Button pop = (Button) findViewById(R.id.popBtn);  
  24.         pop.setOnClickListener(popClick);  
  25.     }  
  26.   
  27.     // 点击弹出左侧菜单的显示方式  
  28.     OnClickListener popClick = new OnClickListener() {  
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             // TODO Auto-generated method stub  
  32.             getPopupWindow();  
  33.             // 这里是位置显示方式,在按钮的左下角  
  34.             popupWindow.showAsDropDown(v);  
  35.             // 这里可以尝试其它效果方式,如popupWindow.showAsDropDown(v,  
  36.             // (screenWidth-dialgoWidth)/2, 0);  
  37.             // popupWindow.showAtLocation(findViewById(R.id.layout),  
  38.             // Gravity.CENTER, 0, 0);  
  39.         }  
  40.     };  
  41.   
  42.     /** 
  43.      * 创建PopupWindow 
  44.      */  
  45.     protected void initPopuptWindow() {  
  46.         // TODO Auto-generated method stub  
  47.         // 获取自定义布局文件activity_popupwindow_left.xml的视图  
  48.         View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,  
  49.                 false);  
  50.         // 创建PopupWindow实例,200,150分别是宽度和高度  
  51.         popupWindow = new PopupWindow(popupWindow_view, 200150true);  
  52.         // 设置动画效果  
  53.         popupWindow.setAnimationStyle(R.style.AnimationFade);  
  54.         // 点击其他地方消失  
  55.         popupWindow_view.setOnTouchListener(new OnTouchListener() {  
  56.             @Override  
  57.             public boolean onTouch(View v, MotionEvent event) {  
  58.                 // TODO Auto-generated method stub  
  59.                 if (popupWindow != null && popupWindow.isShowing()) {  
  60.                     popupWindow.dismiss();  
  61.                     popupWindow = null;  
  62.                 }  
  63.                 return false;  
  64.             }  
  65.         });  
  66.         // activity_popupwindow_left.xml视图里面的控件  
  67.         Button open = (Button) popupWindow_view.findViewById(R.id.open);  
  68.         Button save = (Button) popupWindow_view.findViewById(R.id.save);  
  69.         Button close = (Button) popupWindow_view.findViewById(R.id.close);  
  70.         // activity_popupwindow_left.xml视图里面的控件触发的事件  
  71.         // 打开  
  72.         open.setOnClickListener(new OnClickListener() {  
  73.             @Override  
  74.             public void onClick(View v) {  
  75.                 // TODO Auto-generated method stub  
  76.                 // 这里可以执行相关操作  
  77.                 Toast.makeText(PopwindowLeft.this"打开操作"1000);  
  78.                 // 对话框消失  
  79.                 popupWindow.dismiss();  
  80.             }  
  81.         });  
  82.         // 保存  
  83.         save.setOnClickListener(new OnClickListener() {  
  84.             @Override  
  85.             public void onClick(View v) {  
  86.                 // TODO Auto-generated method stub  
  87.                 // 这里可以执行相关操作  
  88.                 Toast.makeText(PopwindowLeft.this"保存操作"1000);  
  89.                 popupWindow.dismiss();  
  90.             }  
  91.         });  
  92.         // 关闭  
  93.         close.setOnClickListener(new OnClickListener() {  
  94.             @Override  
  95.             public void onClick(View v) {  
  96.                 // TODO Auto-generated method stub  
  97.                 // 这里可以执行相关操作  
  98.                 Toast.makeText(PopwindowLeft.this"关闭操作"1000);  
  99.                 popupWindow.dismiss();  
  100.             }  
  101.         });  
  102.     }  
  103.   
  104.     /*** 
  105.      * 获取PopupWindow实例 
  106.      */  
  107.     private void getPopupWindow() {  
  108.         if (null != popupWindow) {  
  109.             popupWindow.dismiss();  
  110.             return;  
  111.         } else {  
  112.             initPopuptWindow();  
  113.         }  
  114.     }  
  115. }  

PopupWindow主配置文件

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

PopupWindow弹出菜单

[html]   view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@android:color/darker_gray"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/open"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@android:color/darker_gray"  
  13.         android:text="打开" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/save"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:background="@android:color/darker_gray"  
  20.         android:text="保存" />  
  21.   
  22.     <Button  
  23.         android:id="@+id/close"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="@android:color/darker_gray"  
  27.         android:text="关闭" />  
  28.   
  29. </LinearLayout>  

弹出动画XML

在res文件夹下,建立anim文件夹。写入如下两个文件。
弹出动画
in_lefttoright.xml
[html]   view plain copy
  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 plain copy
  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 plain copy
  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>  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值