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

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

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

MainActivity.java

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

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. <Button
  8. android:id="@+id/button1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:gravity="center"
  14. android:background="#C0C0C0"
  15. android:text="点击下拉列表" />
  16. </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. <Button
  7. android:id="@+id/button2"
  8. android:layout_width="200dp"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:paddingRight="70dp"
  13. android:text="viviens" />
  14. <Button
  15. android:id="@+id/button3"
  16. android:layout_width="200dp"
  17. android:layout_height="wrap_content"
  18. android:layout_alignParentLeft="true"
  19. android:layout_below="@+id/button2"
  20. android:paddingRight="70dp"
  21. android:text="mryang" />
  22. <Button
  23. android:id="@+id/button4"
  24. android:layout_width="200dp"
  25. android:layout_height="wrap_content"
  26. android:layout_alignParentLeft="true"
  27. android:layout_below="@+id/button3"
  28. android:paddingRight="70dp"
  29. android:text="张晓达" />
  30. </RelativeLayout>

动画效果:

inputodown.xml 进入屏幕

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <translate
  4. android:duration="500"
  5. android:fromYDelta="-100%"
  6. android:toYDelta="0" />
  7. </set>

outdowntoup.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <translate
  4. android:duration="500"
  5. android:fromYDelta="0"
  6. android:toYDelta="-100%" />
  7. </set>

styles.xml

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

实现效果:

demo地址:

http://download.csdn.net/detail/mad1989/5518035

【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单的更多相关文章

  1. 有序无序ul->li ol->li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单

    实现这一效果利用css和js技术结合 以ul->li为例子 <!DOCTYPE html><html lang="en"><head> & ...

  2. 有序无序Ul->Li Ol->Li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单(变形2 ---修饰)

    从上面可以看出,两个问题,第一:下拉出现的太快太突然,第二:再点击下一个下拉菜单的时候,上一个不会闭合,针对这两个问题,接下来会一 一解决. 解决下拉太快: js中有个jquery效果,有一个效果是j ...

  3. easyui combobox点击输入框弹出下拉框

    由于easyui combobox需要点击下拉箭头才能下拉,不能像select标签那样点击输入框就下拉,所以觉得不太方便,查看了一下,combobox弹出框是一个div,原本想在他的输入框的点击事件中 ...

  4. 【Android初级】如何实现一个有动画效果的自定义下拉菜单

    我们在购物APP里面设置收货地址时,都会有让我们选择省份及城市的下拉菜单项.今天我将使用Android原生的 Spinner 控件来实现一个自定义的下拉菜单功能,并配上一个透明渐变动画效果. 要实现的 ...

  5. jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单

    jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单 截图: 代码如下: //关闭用户菜单 $(document).mousedown(function(e){ var _con = ...

  6. android开发之 包含EditText组件 禁止自动获取焦点弹出输入法

    在EditText标签的外层Layout中加入focusableInTouchMode属性   android:focusableInTouchMode="true" 即可.

  7. 页面点击关闭弹出提示js代码

    代码效果为: <script> window.onbeforeunload = function() { return "您好!\n我是abc\n —————————————— ...

  8. ActionBar点击弹出下拉框操作

    首先: getActionBar().setDisplayShowTitleEnabled(false); ActionBar.LayoutParams lp = new ActionBar.Layo ...

  9. Bootstrap学习笔记(5)--实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单

    实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单 微笑的鱼 2014-01-03 Bootstrap 5,281 次围观 11条评论 使用Bootstrap导航条组件时,如果你的导航条带有下拉 ...

随机推荐

  1. [转]C#中图片.BYTE[]和base64string的转换

    本文转自:http://blog.csdn.net/thebesttome/article/details/6870155 在C#中 图片到byte[]再到base64string的转换: Bitma ...

  2. 学习mfc书籍

    Visual C++ and MFC Programming http://www.math.hcmuns.edu.vn/~tatuana/C%20For%20Win/MFC/Tai%20Lieu%2 ...

  3. TortoiseSVN解决冲突

    解决冲突Resolving Conflicts有时候,你从仓库更新文件时会发生一些冲突.当两个或更多开发人员对同一个文件的某几行做 了修改,就会产生冲突.因为Subversion对你的项目一无所知,他 ...

  4. What is the fastest way of (not) logging?

    原文地址:http://www.slf4j.org/faq.html#logging_performance SLF4J supports an advanced feature called par ...

  5. thymleaf th:if标签

    1.概念 <table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOC ...

  6. go语言基础之break和continue的区别

    1.break和continue的区别 在循环里面有两个关键操作break和continue,break操作是跳出当前循环,continue是跳过本次循环. 2.break 备注:break可⽤于fo ...

  7. 一个十分简洁实用的MD风格的UI主框架

    2017-5-23 详见:https://github.com/baiqiantao/CheesesquareSample MainActivity public class MainActivity ...

  8. .NET中的六个重要概念

    内容导读 概述 当你声明一个变量背后发生了什么? 堆和栈 值类型和引用类型 哪些是值类型,哪些是引用类型? 装箱和拆箱 装箱和拆箱的性能问题 一.概述 本文会阐述六个重要的概念:堆.栈.值类型.引用类 ...

  9. java学习笔记10--泛型总结

    java学习笔记系列: java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Object类 java学习笔记5-- ...

  10. MyEclipse 全面的快捷键

    摘自: http://www.360doc.com/content/11/0406/10/6704374_107513559.shtml 引用 MyEclipse快捷键(全面) 程序代码自动排版:Ct ...