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

没有太多花样,也没有很复杂的技术,就是简单的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>

实现效果:

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

demo地址:

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

上一篇:使用aiohttp实现爬虫


下一篇:Cinder组件详解