歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之用PopupWindow實現彈出listview形式菜單

Android之用PopupWindow實現彈出listview形式菜單

日期:2017/3/1 10:21:09   编辑:Linux編程

Android 4.0之前的菜單使用非常廣泛,但是在android4.0之後,很少使用先前的菜單樣式了。那如何實現下圖的樣式了?

我們簡單模擬一下。

(1)屏蔽系統彈出的菜單:

1、首先創建至少一個系統的菜單選項

  1. @Override
  2. public boolean onCreateOptionsMenu(Menu menu)
  3. {
  4. menu.add("menu");// 必須創建一項
  5. return super.onCreateOptionsMenu(menu);
  6. }

2、在onMenuOpened方法裡顯示自己的菜單視圖,並返回FALSE。 注意必須返回false,不然會出現menu選項

  1. @Override
  2. public boolean onMenuOpened(int featureId, Menu menu){
  3. switchSysMenuShow();
  4. return false;// 返回為true 則顯示系統menu
  5. }

3、從圖片中可以看出,彈出的是一個listview,所以要按照listview的標准來實現布局,給出布局文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:paddingLeft="20dip"
  7. android:gravity="right"
  8. android:id="@+id/hotalk_menu_view_layout" >
  9. <!-- 顯示的listview -->
  10. <ListView android:id="@+id/hotalk_menu_listview"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:paddingLeft="7.6dip"
  14. android:paddingRight="7.6dip"
  15. android:fadingEdge="none"
  16. android:focusable="true"
  17. android:longClickable="true"
  18. android:scrollbarSize="0sp"
  19. android:scrollbarStyle="insideOverlay"
  20. android:background="@drawable/menu_bg_popup"
  21. android:divider="@drawable/menu_bg_line"
  22. android:dividerHeight="1px"
  23. android:cacheColorHint="#00000000">
  24. </ListView>
  25. </RelativeLayout>
Copyright © Linux教程網 All Rights Reserved