zoukankan      html  css  js  c++  java
  • 【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单

     

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


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



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

    MainActivity.java

    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, 0, 5);  
    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.                 null, false);  
    40.         // 创建PopupWindow实例,200,150分别是宽度和高度  
    41.         popupwindow = new PopupWindow(customView, 250, 280);  
    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

    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

    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 进入屏幕

    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

    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

    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


  • 相关阅读:
    html页面转成jsp页面之后样式变化的问题解决方法
    servlet数据库验证登录
    《基于 JSP 技术的试题库系统的设计与实现》10
    《通用试题库管理系统的设计与实现》9
    《基于多目标粒子群算法的智能组卷研究》8
    《基于改进随机抽取算法的信息论题库和智能组卷系统的设计与实现》7
    《高校试题库管理系统的设计与分析》6
    《试题库管理系统的设计与开发》
    《基于 B/S 模式的试题库管理系统的设计与实现 》笔记
    《通用试题库管理系统的设计与开发》笔记
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3884930.html
Copyright © 2011-2022 走看看