【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单

来源:互联网 发布:怎么找网络水军 编辑:程序博客网 时间:2024/05/01 13:56

http://blog.csdn.net/mad1989/article/details/9024977


没有太多花样,也没有很复杂的技术,就是简单的PopupWindow的使用,可以实现点击弹出一个自定义的view,view里可以随便设计,常用的可以放一个listview。


demo中我只是一个点击展示,简单的使用了fade in out的动画效果,也没有精美的图片资源,看着也丑,不过这么短的时间,让你掌握一个很好用的技术,可以自己扩展,不很好么?



废话不说了,直接上代码:

MainActivity.java

[java] view plaincopy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private PopupWindow popupwindow;  
  4.     private Button button;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         button = (Button) findViewById(R.id.button1);  
  12.         button.setOnClickListener(this);  
  13.   
  14.     }  
  15.   
  16.     @Override  
  17.     public void onClick(View v) {  
  18.   
  19.         switch (v.getId()) {  
  20.         case R.id.button1:  
  21.             if (popupwindow != null&&popupwindow.isShowing()) {  
  22.                 popupwindow.dismiss();  
  23.                 return;  
  24.             } else {  
  25.                 initmPopupWindowView();  
  26.                 popupwindow.showAsDropDown(v, 05);  
  27.             }  
  28.             break;  
  29.         default:  
  30.             break;  
  31.         }  
  32.   
  33.     }  
  34.   
  35.     public void initmPopupWindowView() {  
  36.   
  37.         // // 获取自定义布局文件pop.xml的视图  
  38.         View customView = getLayoutInflater().inflate(R.layout.popview_item,  
  39.                 nullfalse);  
  40.         // 创建PopupWindow实例,200,150分别是宽度和高度  
  41.         popupwindow = new PopupWindow(customView, 250280);  
  42.         // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]  
  43.         popupwindow.setAnimationStyle(R.style.AnimationFade);  
  44.         // 自定义view添加触摸事件  
  45.         customView.setOnTouchListener(new OnTouchListener() {  
  46.   
  47.             @Override  
  48.             public boolean onTouch(View v, MotionEvent event) {  
  49.                 if (popupwindow != null && popupwindow.isShowing()) {  
  50.                     popupwindow.dismiss();  
  51.                     popupwindow = null;  
  52.                 }  
  53.   
  54.                 return false;  
  55.             }  
  56.         });  
  57.   
  58.         /** 在这里可以实现自定义视图的功能 */  
  59.         Button btton2 = (Button) customView.findViewById(R.id.button2);  
  60.         Button btton3 = (Button) customView.findViewById(R.id.button3);  
  61.         Button btton4 = (Button) customView.findViewById(R.id.button4);  
  62.         btton2.setOnClickListener(this);  
  63.         btton3.setOnClickListener(this);  
  64.         btton4.setOnClickListener(this);  
  65.   
  66.     }  
  67.   
  68. }  


activity_main.xml

[java] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#000000"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:gravity="center"  
  15.         android:background="#C0C0C0"  
  16.         android:text="点击下拉列表" />  
  17.   
  18. </RelativeLayout>  


自定义view的xml

[java] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#C0C0C0" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button2"  
  9.         android:layout_width="200dp"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:paddingRight="70dp"  
  14.         android:text="viviens" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/button3"  
  18.         android:layout_width="200dp"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentLeft="true"  
  21.         android:layout_below="@+id/button2"  
  22.         android:paddingRight="70dp"  
  23.         android:text="mryang" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/button4"  
  27.         android:layout_width="200dp"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentLeft="true"  
  30.         android:layout_below="@+id/button3"  
  31.         android:paddingRight="70dp"  
  32.         android:text="张晓达" />  
  33.   
  34. </RelativeLayout>  


动画效果:

inputodown.xml 进入屏幕

[java] view plaincopy
  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:fromYDelta="-100%"  
  7.         android:toYDelta="0" />  
  8.   
  9. </set>  

outdowntoup.xml
[java] view plaincopy
  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:fromYDelta="0"  
  7.         android:toYDelta="-100%" />  
  8.   
  9. </set>  


styles.xml

[html] view plaincopy
  1. <style name="AnimationFade">  
  2.   
  3.     <!-- PopupWindow左右弹出的效果 -->  
  4.     <item name="android:windowEnterAnimation">@anim/inuptodown</item>  
  5.     <item name="android:windowExitAnimation">@anim/outdowntoup</item>  
  6. </style>  




实现效果:



demo地址:

http://download.csdn.net/detail/mad1989/5518035
0 0
原创粉丝点击