1、PopupWindow是一個可以顯示在當前Activity之上的浮動容器,PopupWindow彈出的位置是能夠改變的,按照有無偏移量,可以分為無偏移和有便宜兩種;按照參照對象的不同又可以分為兩種:相對某個控件(Anchor錨點)的位置和在父容器內部的相對位置。
顯示PopupWindow的方法:
showAsDropDown(Viewanchor) //相對某個控件的位置(正下方),無偏移 showAsDropDown(Viewanchor, int xoff, int yoff) //相對某個控件的位置,有偏移,xoff X軸的偏移量,yoff Y軸的偏移量 showAtLocation(Viewparent, int gravity, int x, int y) //在父容器的什么位置,gravity為相對位置,如:正中央Gravity.CENTER、下方Gravity.BOTTOM、 Gravity.Right|Gravity.BOTTOM右下方等,后面兩個參數為x/y軸的偏移量。
2、創建泡泡窗口的界面:
android:verticalSpacing="10px" //垂直間隔
android:horizontalSpacing="10px" //橫向間隔
android:numColumns="auto_fit" //自適應列數
android:columnWidth="60px" //列寬
android:stretchMode="columnWidth" //縮放與列寬大小同步
convertView.setLayoutParams(new AbsListView.LayoutParams(45,45)); //有時候需要指定格子的寬度和高度
<?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:background="@drawable/bg" android:orientation="vertical" > <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="10dp" android:numColumns="4" android:verticalSpacing="10dp" /> </LinearLayout>
為GridView控件設置背景色,在res/drawable目錄創建bg.xml文件:
<?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endColor="#1DC9CD" android:startColor="#A2E0FB" /> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /> </shape>
泡泡窗口由圖片和文字組成,因此為它的項設置布局界面grid_item.xml文件:
<?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" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000099" android:textSize="16sp" /> </LinearLayout>
3、為泡泡窗口設置動畫效果:
設置動畫樣式:
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> <style name="animation"> <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/out</item> </style> </resources>
動畫樣式需要在res/anim下創建,需要創建進入(enter.xml)和退出(out.xml)兩個動畫:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <!-- 設置平移,現在只需要在Y軸上移動 --> <translate android:duration="500" android:fromYDelta="100%p" android:toYDelta="0" /> <!-- 設置透明度 --> <alpha android:duration="300" android:fromAlpha="0.7" android:toAlpha="1.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <translate android:duration="3000" android:fromYDelta="0" android:toYDelta="100%p" /> <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.5" /> </set>
4、后台代碼實現:
package com.example.popupwindow; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ListAdapter; import android.widget.PopupWindow; import android.widget.SimpleAdapter; public class MainActivity extends Activity { PopupWindow popupWindow; View parent; private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7, R.drawable.i8 }; private String[] names = { "搜索", "文件管理", "下載管理", "全屏", "網址", "書簽", "加入書簽", "分享頁面" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // LayoutInflater mLayoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE); //通過LayoutInflater獲取布局界面 View contentView = getLayoutInflater().inflate(R.layout.popupwin, null); //查找popupwin界面的GridView控件 GridView gridView = (GridView) contentView.findViewById(R.id.gridView); //填充數據 gridView.setAdapter(getAdapter()); //設置點擊事件 gridView.setOnItemClickListener(new ItemClickListener()); popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true);// 取得焦點 // 點擊空白的地方關閉PopupWindow popupWindow.setBackgroundDrawable(new BitmapDrawable()); //給popupWindow窗口加入動畫效果 popupWindow.setAnimationStyle(R.style.animation); parent = this.findViewById(R.id.main); } private final class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (popupWindow.isShowing()) popupWindow.dismiss();// 關閉 // .... } } //將圖片和名稱字段對應到視圖界面grid_view的圖像和文本上 private ListAdapter getAdapter() { List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < images.length; i++) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("image", images[i]); item.put("name", names[i]); data.add(item); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.grid_item, new String[] { "image", "name" }, new int[] { R.id.imageView, R.id.textView }); return simpleAdapter; } //打開PopupWindow窗口 public void openPopWindow(View v) { //在父窗口的底部顯示,沒有偏移量 popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
相對某個view的位置顯示:
lv_app_manager.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 獲取當前view對象在窗體中的位置 int[] arrayOfInt = new int[2]; view.getLocationInWindow(arrayOfInt); int i = arrayOfInt[0] + 60; int j = arrayOfInt[1]; View popupview = View.inflate(AppManagerActivity.this, R.layout.popup_item, null); LinearLayout ll_start = (LinearLayout) popupview .findViewById(R.id.ll_start); LinearLayout ll_uninstall = (LinearLayout) popupview.findViewById(R.id.ll_uninstall); LinearLayout ll_share = (LinearLayout) popupview.findViewById(R.id.ll_share); // 把當前條目在listview中的位置設置給view對象 ll_share.setTag(position); ll_uninstall.setTag(position); ll_start.setTag(position); ll_start.setOnClickListener(AppManagerActivity.this); ll_uninstall.setOnClickListener(AppManagerActivity.this); ll_share.setOnClickListener(AppManagerActivity.this); LinearLayout ll = (LinearLayout) popupview.findViewById(R.id.ll_popup); ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
sa.setDuration(200); localPopupWindow = new PopupWindow(popupview, 230, 70); // 一定要記得給popupwindow設置背景顏色 // Drawable background = new ColorDrawable(Color.TRANSPARENT); Drawable background = getResources().getDrawable( R.drawable.local_popup_bg); localPopupWindow.setBackgroundDrawable(background); localPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, i, j); ll.startAnimation(sa); } });
private void showPopUp(View v) { LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.GRAY); TextView tv = new TextView(this); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tv.setText("I'm a pop -----------------------------!"); tv.setTextColor(Color.WHITE); layout.addView(tv); popupWindow = new PopupWindow(layout,120,120); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()); }
pop = new PopupWindow(
layout,
myButton.getWidth(),
myButton.getHeight() * 3 + 5);
ColorDrawable cd = new ColorDrawable(-0000);
pop.setBackgroundDrawable(cd);
// pop.showAsDropDown(v);
pop.update();
pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
pop.setTouchable(true); // 設置popupwindow可點擊
pop.setOutsideTouchable(true); // 設置popupwindow外部可點擊
pop.setFocusable(true); //獲取焦點
/*設置popupwindow的位置*/
pop.showAtLocation(layout,
(Gravity.BOTTOM - myButton.getHeight())
| Gravity.LEFT, 0, 2 * myButton.getHeight());
state = 1;
pop.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
/**** 如果點擊了popupwindow的外部,popupwindow也會消失 ****/
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pop.dismiss();
return true;
}
return false;
}
});
由於手機分辨率不同,設置popwindow的寬度和高度時,如果給定數字,那么可能會在模擬器和手機上產生不同的效果。因此可以采用dip與px轉換,也可以先獲取手機的屏幕寬度和高度,根據寬度和高度進行設置。
public class DensityUtil { /** * 根據手機的分辨率從 dp 的單位 轉成為 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根據手機的分辨率從 px(像素) 的單位 轉成為 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); // mPopupWindow = new PopupWindow(popupLayout,metrics.widthPixels/3,+metrics.heightPixels/2);//采用手機寬度和高度進行設置 mPopupWindow = new PopupWindow(popupLayout,DensityUtil.dip2px(this, 150), DensityUtil.dip2px(this, 200));//通過類進行轉換
半透明彈出框PopUpWindow http://www.apkbus.com/android-2474-1-1.html PopUpWindow實現半透明彈出框關鍵點: 布局文件 最外層設置為全屏 背景顏色為半透明 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/translucent" android:gravity="center" android:orientation="vertical" > ....... <ListView android:id="@+id/qianghaoqi_diary_pop_list" android:divider="@null" android:scrollingCache="false" android:fadingEdge="none" android:scrollbarThumbVertical="@drawable/game_blade_qianghaoqi_listview_scrollbar" android:layout_marginBottom="@dimen/dip5" android:layout_marginLeft="@dimen/dip5" android:layout_marginRight="@dimen/dip10" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:listSelector="@null" android:paddingLeft="15.0dip" /> ......... </RelativeLayout> new一個全屏的PopUpWindow //必須為true,可以獲取焦點 mPopWin = new PopupWindow(aPopView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,true); //mPopWin.setHeight(450);//設置PopupWindow高度 //必須設置。改變彈出窗口的背景,當然也可以設置為NULL。 mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg)); mPopWin.showAtLocation(aParentView,Gravity.CENTER, 0, 0); //如果窗口已經顯示過,更改此值只能在下一次顯示時起作用,或者調用update() mPopWin.update(); 如果PopUpWindow內的布局會奪取焦點(如示例ListView),注意代碼 mListView.requestFocus(); // 焦點到了listView上,所以需要監聽此處的鍵盤事件。否則會出現不響應鍵盤事件的情況 mListView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { closePopWin(); } return true; } }); 關閉PopUpWindow private void closePopWin(){ if (mPopWin != null && mPopWin.isShowing()) { mPopWin.dismiss(); } } PopUpWindow一般應用 mPopWin = new PopupWindow(mPopView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg)); mPopWin.setOutsideTouchable(true); mPopWin.setFocusable(true); mPopWin.showAsDropDown(aParentView,0,-15); mPopWin.update(); 0 熱度|全文鏈接 #PopWin#半透明 Android之PopWindow 1.設置半透明主題 2.設置window的alpha值 // WindowManager.LayoutParams lp =getWindow().getAttributes(); // lp.alpha =0.5f; //0.0-1.0 // getWindow().setAttributes(lp); 發現這兩種都不能滿足要求,起碼的顏色就不太對。想做好點,做成類似alertDialog的樣子,帶邊框,彈出窗口帶動畫效果,之后背景置灰,那多帥。 看到那個仿uc瀏覽器的源碼,是用alertdialog做的,達到那種效果,加點動畫就行了。下圖是從那個ucweb源碼里面弄出來的。 上面的代碼就不貼了,我上傳的項目文件里面也有。 下面是彈出popupwindow的圖片,第一張是動畫中,第二張是完全彈出的: 彈出popwindow的代碼如下,比較亂,多包涵: popupWindow = new PopupWindow(menuView,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true); popupWindow.showAtLocation(findViewById(R.id.parent),Gravity.CENTER |Gravity.CENTER, 0, 0); popupWindow.setAnimationStyle(R.style.PopupAnimation); //加上下面兩行可以用back鍵關閉popupwindow,否則必須調用dismiss(); ColorDrawable dw = new ColorDrawable(-00000); popupWindow.setBackgroundDrawable(dw); popupWindow.update(); 下面是實現步驟: 1。背景置灰: popupWindow =new PopupWindow(menuView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true); 第二三個參數必須是LayoutParams.FILL_PARENT,這樣才能填充整個屏幕,達到背景置灰的目的。 整個popupwindow里面是一個GridView,圖片什么的也是用的那個仿UC瀏覽器界面項目的,在此謝謝了。 關鍵的東西都在xml里面。 <?xml version="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:gravity="center"android:layout_height="fill_parent" android:layout_gravity="center"android:background="#b0000000" > <LinearLayoutandroid:orientation="vertical" android:layout_width="wrap_content"android:gravity="center" android:layout_height="wrap_content"android:layout_gravity="center" android:background="@drawable/downbutton_corner"> <GridViewandroid:id="@+id/gridview"android:layout_width="wrap_content" android:layout_height="wrap_content"android:numColumns="4" android:verticalSpacing="5dip"android:horizontalSpacing="5dip" android:stretchMode="columnWidth"android:gravity="center" android:layout_gravity="center"/></LinearLayout></LinearLayout> 第一個linearlayout里面的android:background="#b0000000",就是全屏背景,網上搜的好多半透明都是“#e0000000”,我覺得那顏色太深,“#b0000000”更合適。 第二個linearlayout是popupwind的背景,里面的android:background="@drawable/downbutton_corner"是關鍵,邊框,圓角都是里面定義的。 2。popupwindow的邊框,圓角背景。downbutton_corne.xml <shapexmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradientandroid:startColor="#c0000000"android:endColor="#c0000000" android:angle="90" /><!--背景顏色漸變--> <strokeandroid:dashWidth="2dp" android:dashGap="2dp" android:width="2dp"android:color="#FF00ff00"></stroke> <!--描邊--> <cornersandroid:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"android:topLeftRadius="5dp" android:topRightRadius="5dp"/><!--設置圓角--> </shape> 這個涉及到shape畫圖,要是不懂的話。網上很多資料,搜一下就是了。我博客里面也有,http://blog.csdn.net/ymdcr/archive/2010/12/01/6048256.aspx <gradient android:startColor="#c0000000"android:endColor="#c0000000" android:angle="90"/><!--背景顏色漸變 --> 我就設置了一個固定的顏色"#c0000000"。android:angle="90"這個是設置顏色漸變方向,從上到下啊,從左到右啊,貌似只能90的倍數,也只有四個方向嘛。 <stroke></stroke>,邊框就是這個實現的。 dashWidth指的是邊線的寬度 dashGap 指的是每條線之間的間距,(因為是邊線是很多小橫線組成的)。 3。淡入淡出動畫 popupWindow.setAnimationStyle(R.style.PopupAnimation); 這條代碼是設置style的,動畫文件就是在style文件里面引入的。下面是淡入的動畫,動畫教程網上也很多。淡出的動畫就這些參數值交換位置就是了。android:duration這個是持續時間,為了截圖,我把它弄成5秒了。 <setxmlns:android="http://schemas.android.com/apk/res/android"> <scaleandroid:fromXScale="0.6" android:toXScale="1.0" android:fromYScale="0.6" android:toYScale="1.0"android:pivotX="50%" android:pivotY="50%" android:duration="5000"/> <alphaandroid:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0"android:duration="5000" /> </set> 復制代碼 大概就是這些了。 還有一個關鍵的問題。彈出pop之后,back鍵無效了,必須在pop里面設置事件dismiss掉。下面是問題的描述,哪位解決了,告訴我一下,謝謝。我的郵箱: 問題解決了,是因為沒設置背景的原因。 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //把這一行放在showAtLocation前面就行了,以前是放在后面的,粗心了。 popupWindow.showAtLocation(findViewById(R.id.parent),Gravity.CENTER |Gravity.CENTER, 0,0); 網上也有很多人說,彈出pop之后,不響應鍵盤事件了,這個其實是焦點在pop里面的view去了。 以這個為例,焦點就在gridview上面去了。28樓的兄弟提示的,謝了。 在gridview加上setOnKeyListener,就能解決。 menuGrid.setOnKeyListener(newOnKeyListener() { @Override publicboolean onKey(View v, int keyCode, KeyEvent event){ switch(keyCode) { caseKeyEvent.KEYCODE_MENU: if(popupWindow != null &&popupwindows.isShowing()) { popupWindow.dismiss(); System.out.println("menuGridfdsfdsfdfd"); } break; } returntrue; } }); --------------------------------------------------------------------------------- 使用PopupWindow來做自定義menu,往PopupWindow增加一個子View,子View的布局就是menu的布局。 出現和退出的動畫:可以給PopUpWindow或它的子view添加。 網上所有用PopupWindow做的menu有個共同特點:就是點擊menu鍵出現PopupWindow,然后再點擊menu鍵無法使PopupWindow退出/dismiss()。 當給PopupWindow設置了setFocusable(true),menu顯示后,點擊menu其他任何地方,menu都會消失,但是這時候按鈕的點擊事件其實是不響應的。同時只響應鍵盤的返回鍵,其他按鍵均不響應,比如點擊menu鍵,沒有任何反應。 要解決這個問題很簡單,就是給PopupWindow的子View設置下面的代碼: [java] view plaincopy //sub_view 是PopupWindow的子View sub_view.setFocusableInTouchMode(true); sub_view.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) { mPopupWindow.dismiss();// 這里寫明模擬menu的PopupWindow退出就行 return true; } return false; } }); 記住,一定要給PopupWindow設置setFocusable(true),要不然點擊menu其他地方以及返回鍵,menu都不會退出。且這時候是響應PopupWindow的parent的menu事件的。 下面闡述為什么這么寫之后,當PopupWindow顯示后,點擊menu鍵PopupWindow會退出的原因: 首先得明白為什么給PopupWindow setFocusable(true)后,點擊menu出現PopupWindow后再點擊menu沒反應的原因。 PopupWindow初始化的時候一般都指定了在哪個View上出現,我們稱這個View為parent。parent里面寫了點擊menu出現PopupWindow的事件,如果給PopupWindow setFocusable(true),此時屏幕的焦點在PopupWindow上面,肯定是不會響應parent的按鍵事件的,它只會響應PopupWindow的按鍵事件。 但是PopupWindow的本質是Window,沒有繼承View類,自己沒有onkeyDown或onkey或dispatchKey這些事件的。我剛開始試着實現這些接口,但是按鍵依然不響應,不知原因。因現在對按鍵的原理還不熟,無法闡述其原因。 然后我想繞道而行,就是給PopupWindow的子View注冊按鍵事件,setKeyListener,剛開始我在子View的xml設置了android:focusable=”true” 但按鍵事件依然不響應。。。。糾結啊糾結。。。然后沒得辦法,我google了所有關於PopupWindow的文章。。。最后終於被我發現。。。需要給PopupWindow的子View 設置setFocusableInTouchMode(true)。這時候按鍵事件就響應了。。。 下面附上完整代碼: [java] view plaincopy /*必須重寫,否則點擊MENU無反應 為了讓他不顯示,下面onMenuOpened()必須返回false*/ @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("menu");// 必須創建一項 return super.onCreateOptionsMenu(menu); } /** * 攔截MENU */ @Override public boolean onMenuOpened(int featureId, Menu menu) { if(mPopupWindow != null){ if(!mPopupWindow.isShowing()){ /*最重要的一步:彈出顯示 在指定的位置(parent) 最后兩個參數 是相對於 x / y 軸的坐標*/ mPopupWindow.showAtLocation(findViewById(R.id.linear_menu_parent), Gravity.BOTTOM, 0, 0); } } return false;// 返回為true 則顯示系統menu } private void initPopuWindow(int menuViewID){ LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); /*設置顯示menu布局 view子VIEW*/ sub_view = mLayoutInflater.inflate(menuViewID, null); /*第一個參數彈出顯示view 后兩個是窗口大小*/ mPopupWindow = new PopupWindow(sub_view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); /*設置背景顯示*/ mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_menu_popup)); /*設置觸摸外面時消失*/ mPopupWindow.setOutsideTouchable(true); /*設置系統動畫*/ mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); mPopupWindow.update(); mPopupWindow.setTouchable(true); /*設置點擊menu以外其他地方以及返回鍵退出*/ mPopupWindow.setFocusable(true); /** 1.解決再次點擊MENU鍵無反應問題 * 2.sub_view是PopupWindow的子View */ sub_view.setFocusableInTouchMode(true); sub_view.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) { mPopupWindow.dismiss();// 這里寫明模擬menu的PopupWindow退出就行 return true; } return false; } }); /*監聽MENU事件*/ menu = new View[3]; menu[0] = sub_view.findViewById(R.id.menu_0); menu[1] = sub_view.findViewById(R.id.menu_1); menu[2] = sub_view.findViewById(R.id.menu_2); menu[0].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // doSomething } }); menu[1].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // doSomething } }); menu[2].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // doSomething } });
