PopupWindow实现右侧、左侧和底部弹出菜单

来源:互联网 发布:林更新网络一线 编辑:程序博客网 时间:2024/03/29 00:32

先上图,4张:









项目代码:http://download.csdn.net/download/jianfengwen/9124745 (需要2个分)


项目SDK是5.1,建议将代码拷到自己的工程中去


代码如下:


MainActivity类:


[java] view plain copy
  1. package com.example.popupleftmenu;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.Gravity;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.View.OnTouchListener;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.view.WindowManager;  
  14. import android.widget.Button;  
  15. import android.widget.PopupWindow;  
  16. import android.widget.Toast;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private Context context = null;  
  21.     private PopupWindow popupWindow;  
  22.     private int from = 0;  
  23.       
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         context = this;  
  28.         setContentView(R.layout.activity_main);  
  29.         Button popLeftBtn = (Button)findViewById(R.id.pop_left_btn);  
  30.         Button popRightBtn = (Button)findViewById(R.id.pop_right_btn);  
  31.         Button popBottomBtn = (Button)findViewById(R.id.pop_bottom_btn);  
  32.         popLeftBtn.setOnClickListener(popClick);  
  33.         popRightBtn.setOnClickListener(popClick);  
  34.         popBottomBtn.setOnClickListener(popClick);  
  35.     }  
  36.   
  37.       
  38.     OnClickListener popClick = new OnClickListener() {  
  39.           
  40.         @Override  
  41.         public void onClick(View v) {  
  42.             switch(v.getId()){  
  43.                 case R.id.pop_left_btn:{  
  44.                     from = Location.LEFT.ordinal();  
  45.                     break;  
  46.                 }  
  47.                 case R.id.pop_right_btn:{  
  48.                     from = Location.RIGHT.ordinal();  
  49.                     break;  
  50.                 }  
  51.                 case R.id.pop_bottom_btn:{  
  52.                     from = Location.BOTTOM.ordinal();  
  53.                     break;  
  54.                 }  
  55.             }  
  56.               
  57.             //调用此方法,menu不会顶置  
  58.             //popupWindow.showAsDropDown(v);  
  59.             initPopupWindow();  
  60.               
  61.         }  
  62.     };  
  63.     /**  
  64.      * 添加新笔记时弹出的popWin关闭的事件,主要是为了将背景透明度改回来  
  65.      *  
  66.      */   
  67.     class popupDismissListener implements PopupWindow.OnDismissListener{  
  68.   
  69.         @Override  
  70.         public void onDismiss() {  
  71.             backgroundAlpha(1f);  
  72.         }  
  73.           
  74.     }  
  75.       
  76.       
  77.     protected void initPopupWindow(){  
  78.         View popupWindowView = getLayoutInflater().inflate(R.layout.pop, null);  
  79.         //内容,高度,宽度  
  80.         if(Location.BOTTOM.ordinal() == from){  
  81.             popupWindow = new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);  
  82.         }else{  
  83.             popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, true);  
  84.         }  
  85.         //动画效果  
  86.         if(Location.LEFT.ordinal() == from){  
  87.             popupWindow.setAnimationStyle(R.style.AnimationLeftFade);  
  88.         }else if(Location.RIGHT.ordinal() == from){  
  89.             popupWindow.setAnimationStyle(R.style.AnimationRightFade);  
  90.         }else if(Location.BOTTOM.ordinal() == from){  
  91.             popupWindow.setAnimationStyle(R.style.AnimationBottomFade);  
  92.         }  
  93.         //菜单背景色  
  94.         ColorDrawable dw = new ColorDrawable(0xffffffff);  
  95.         popupWindow.setBackgroundDrawable(dw);  
  96.         //宽度  
  97.         //popupWindow.setWidth(LayoutParams.WRAP_CONTENT);   
  98.         //高度  
  99.         //popupWindow.setHeight(LayoutParams.FILL_PARENT);  
  100.         //显示位置  
  101.         if(Location.LEFT.ordinal() == from){  
  102.             popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.LEFT, 0500);  
  103.         }else if(Location.RIGHT.ordinal() == from){  
  104.             popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.RIGHT, 0500);  
  105.         }else if(Location.BOTTOM.ordinal() == from){  
  106.             popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 00);  
  107.         }  
  108.         //设置背景半透明  
  109.         backgroundAlpha(0.5f);  
  110.         //关闭事件  
  111.         popupWindow.setOnDismissListener(new popupDismissListener());  
  112.           
  113.         popupWindowView.setOnTouchListener(new OnTouchListener() {  
  114.               
  115.             @Override  
  116.             public boolean onTouch(View v, MotionEvent event) {  
  117.                 /*if( popupWindow!=null && popupWindow.isShowing()){ 
  118.                     popupWindow.dismiss(); 
  119.                     popupWindow=null; 
  120.                 }*/  
  121.                 // 这里如果返回true的话,touch事件将被拦截  
  122.                 // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss  
  123.                 return false;  
  124.             }  
  125.         });  
  126.           
  127.         Button open = (Button)popupWindowView.findViewById(R.id.open);  
  128.         Button save = (Button)popupWindowView.findViewById(R.id.save);  
  129.         Button close = (Button)popupWindowView.findViewById(R.id.close);  
  130.           
  131.           
  132.         open.setOnClickListener(new OnClickListener() {  
  133.               
  134.             @Override  
  135.             public void onClick(View v) {  
  136.                 Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();  
  137.                 popupWindow.dismiss();  
  138.             }  
  139.         });  
  140.           
  141.         save.setOnClickListener(new OnClickListener() {  
  142.               
  143.             @Override  
  144.             public void onClick(View v) {  
  145.                 Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();  
  146.                 popupWindow.dismiss();  
  147.             }  
  148.         });  
  149.           
  150.         close.setOnClickListener(new OnClickListener() {  
  151.               
  152.             @Override  
  153.             public void onClick(View v) {  
  154.                 Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();  
  155.                 popupWindow.dismiss();  
  156.             }  
  157.         });  
  158.     }  
  159.       
  160.     /** 
  161.      * 设置添加屏幕的背景透明度 
  162.      * @param bgAlpha 
  163.      */  
  164.     public void backgroundAlpha(float bgAlpha)  
  165.     {  
  166.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  167.         lp.alpha = bgAlpha; //0.0-1.0  
  168.         getWindow().setAttributes(lp);  
  169.     }  
  170.     /** 
  171.      * 菜单弹出方向 
  172.      * 
  173.      */  
  174.     public enum Location {  
  175.   
  176.         LEFT,  
  177.         RIGHT,  
  178.         TOP,  
  179.         BOTTOM;  
  180.           
  181.     }  
  182. }  


两个布局文件:


1.activity_main.xml,就三个Button


[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <Button   
  8.         android:id="@+id/pop_left_btn"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/pop_left"/>  
  12.       
  13.     <Button   
  14.         android:id="@+id/pop_right_btn"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/pop_right"/>  
  18.       
  19.     <Button   
  20.         android:id="@+id/pop_bottom_btn"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="@string/pop_bottom"/>  
  24.       
  25.   
  26. </LinearLayout>  


2. pop.xml,也是三个Button,可以自己修改


[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:orientation="vertical" >  
  6.       
  7.     <!-- <LinearLayout   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:background="#ffffff"> -->  
  12.           
  13.         <Button android:id="@+id/open"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:text="@string/open"/>  
  17.           
  18.         <Button android:id="@+id/save"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="@string/save"/>  
  22.           
  23.         <Button android:id="@+id/close"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="@string/close"/>  
  27.           
  28.    <!--  </LinearLayout> -->  
  29.       
  30. </LinearLayout>  


strings.xml


[html] view plain copy
  1. <string name="pop_left">弹出左侧菜单</string>  
  2.     <string name="pop_right">弹出右侧菜单</string>  
  3.     <string name="pop_bottom">弹出底部菜单</string>  
  4.     <string name="open">打开</string>   
  5.     <string name="save">保存</string>   
  6.     <string name="close">关闭</string>   


styles.xml

[html] view plain copy
  1. <style name="AnimationLeftFade">  
  2.         <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>  
  3.         <item name="android:windowExitAnimation">@anim/out_righttoleft</item>  
  4.     </style>  
  5.       
  6.     <style name="AnimationRightFade">  
  7.         <item name="android:windowEnterAnimation">@anim/in_righttoleft</item>  
  8.         <item name="android:windowExitAnimation">@anim/out_lefttoright</item>  
  9.     </style>  
  10.       
  11.     <style name="AnimationBottomFade">  
  12.         <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>  
  13.         <item name="android:windowExitAnimation">@anim/out_toptobottom</item>  
  14.     </style>  


左边弹出菜单动画文件:


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.     <translate   
  5.         android:fromXDelta="-100%"  
  6.         android:toXDelta="0"  
  7.         android:duration="500"/>  
  8.       
  9. </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.     <translate android:fromXDelta="0"  
  5.         android:toXDelta="-100%"  
  6.         android:duration="500"/>  
  7.   
  8. </set>  

其他动画文件自己参考写,就是fromXDelta, fromYDelta, toXDelta和toYDelta使用

转载地址:

http://blog.csdn.net/jianfengwen/article/details/48629217

项目代码:http://download.csdn.net/download/jianfengwen/9124745 (需要2个分)



0 0
原创粉丝点击