本文主要介紹PopupWindow的基本知識、利用PopupWindow實現類似網頁上或者windows開始按鈕的菜單效果以及如何解決PopupWindow和listView或GridView同時使用時焦點及頁面響應問題
因為PopupWindow能實現非模態對話框效果,所以建議大家使用,而不是用AlertDialog等模態對話框阻止用戶的操作.
1、PopupWindow介紹
PopupWindow可以用來裝載一些信息或是View,它可以懸浮在當前活動窗口上,並且不干擾用戶對背后窗口的操作。
1.1 PopupWindow創建方式,主要為
a. 顯示contentView
PopupWindow(View contentView)
b. 顯示contentView,固定的高度和寬度
PopupWindow(View contentView, int width, int height)
更多見PopupWindow
1.2 PopupWindow顯示方式,三種分別為
a. 在anchor的左下角顯示PopupWindow,偏移位置為xoff和yoff,即相當於以anchor的左下角定點為二維坐標系的原點,向右為正x,向上為正y
void showAsDropDown(View anchor, int xoff, int yoff)
b. 在anchor的左下角顯示PopupWindow,偏移位置為0
void showAsDropDown(View anchor)
c. 在固定位置顯示,parent為父view,gravity為顯示的格式(如劇中),x和y為坐標點
void showAtLocation(View parent, int gravity, int x, int y)
1.3 其他可能用到的
焦點,設置PopupWindow是否可以獲得焦點。
輸入法模式setInputMethodMode。有三種為允許輸入法、不允許輸入法、根據是否可以有焦點決定。
2、利用PopupWindow實現菜單效果
2.1 利用listView顯示
// list的數據源 List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map1 = new HashMap<String, String>(); map1.put("menuName", "菜單1"); HashMap<String, String> map2 = new HashMap<String, String>(); map2.put("menuName", "菜單2"); HashMap<String, String> map3 = new HashMap<String, String>(); map3.put("menuName", "菜單3"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(AccountManageActivity.this, list, R.layout.account_manage_long_click_row, new String[] {PopupWindowMenu.FIRST_COLUMN}, new int[] {R.id.accountLongClickTextView}); View view = getLayoutInflater().inflate(R.layout.account_manage_list, null); ListView listView = (ListView)view.findViewById(R.id.listView); listView.setAdapter(listAdapter); // PopupWindow定義,顯示view,以及初始化長和寬 PopupWindow menu = new PopupWindow(view, 200, 60); // 必須設置,否則獲得焦點后頁面上其他地方點擊無響應 menu.setBackgroundDrawable(new BitmapDrawable()); // 設置焦點,必須設置,否則listView無法響應 menu.setFocusable(true); // 設置點擊其他地方 popupWindow消失 menu.setOutsideTouchable(true); // 顯示在某個位置 menu.showAsDropDown(anchor);
其中R.layout.account_manage_list文件為
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
其中R.layout.account_manage_long_click_row文件為
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|left"> <TextView android:id="@+id/accountLongClickTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left|center_vertical" android:layout_marginLeft="4dp" style="@style/DefaultFontStyle"> </TextView> </LinearLayout>
注意必須設置PopupWindow的setFocusable(true),否則listView無法響應,
同時需要設置setBackgroundDrawable(new BitmapDrawable());否則setFocusable(true)后,點擊頁面其他地方便無法響應。
除了setBackgroundDrawable(new BitmapDrawable());還可以使用下面兩種解決頁面無法響應問題
b、最笨的方法將listView中元素拿出來放到LinearLayout中,對於非listView都無需設置setFocusable(true),從而解決問題,具體可以見http://blog.csdn.net/ihrthk/article/details/7338791
但這種方法對於動態變化的菜單需要配置多份layout文件
2.2 利用GridView實現方格菜單
對於很多應用像uc瀏覽器的菜單都有更多的選項和分級,實現道理同上面的listView,可以參考http://blog.csdn.net/kkfdsa132/article/details/6403404
同樣可能碰到上面的響應問題
參考:
http://developer.android.com/reference/android/widget/PopupWindow.html
更多對話框使用可參考
http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html
已有 0 人發表留言,猛擊->>這里<<-參與討論
ITeye推薦