没有太多花样,也没有很复杂的技术,就是简单的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. extjs grid数据改变后刷新的实现

    做了一个编辑extjs grid记录的窗体,但更改数据后,怎么重新刷新grid让数据显示呢? 做了半天的尝试,其实到最后只需一句话,faint:-) this.store.reload(); 不用加任 ...

  2. VS 2017 取消结构参考线的显示

    Visual studio 中的结构参考线如下所示 其可以通过如下方式取消:

  3. iTunes Connect App Bundles

    App Bundles捆绑销售提交流程: 1. 在iTunes Connect左上「+」选「Create Bundle」到「New App Bundle」挑选已上线应用(最多可捆绑10个应用) 2. ...

  4. 如何编译Linux内核

    内核,是一个操作系统的核心.它负责管理系统的进程.内存.设备驱动程序.文件和网络系统,决定着系统的性能和稳定性.Linux作为一个自由软件,在广 大爱好者的支持下,内核版本不断更新.新的内核修订了旧内 ...

  5. Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  6. 集成禅道和svn

    转载:http://www.zentao.net/book/zentaopmshelp/137.html 说明:svn集成功能配置会比较复杂,我们会尽量通过文档来帮助大家配置成功!如果实在配置不成功的 ...

  7. go语言基础之递归实现数字累加

    1.实现1+100 = 5050 示例: package main import "fmt" //实现1+2+3+……100 func test01() (sum int) { f ...

  8. DGN格式转化为shp格式 【转】

    其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK80 ...

  9. J2EE 中 用 El表达式 和 Jsp 方式 取得 URL 中的参数方法

    使用 el表达式方法: var urlParamValue = "${param.urlVarName}"; 使用 Jsp 表达式 var urlParamValue2 = &qu ...

  10. iScroll示例,下拉刷新,上拉刷新

    iScroll示例,下拉刷新,上拉刷新 <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...