部分經常使用: java
PopupWindow(View contentView, int width, int height) Create a new non focusable popup window which can display the contentView. PopupWindow(View contentView, int width, int height, boolean focusable) Create a new popup window which can display the contentView.
contentView爲要顯示的view,width和height爲寬和高,值爲像素值,也能夠是MATCHT_PARENT和WRAP_CONTENT。 android
focusable爲是否能夠得到焦點,也能夠經過set方法設置: ios
// Changes the focusability of the popup window setFocusable(boolean focusable)
根據網上的資料,focusable參數的主要做用是: ide
若是focusable爲false,在一個Activity彈出一個PopupWindow,按返回鍵,因爲PopupWindow沒有焦點,會直接退出Activity。若是focusable爲true,PopupWindow彈出後,全部的觸屏和物理按鍵都由PopupWindow處理。 佈局
若是PopupWindow中有Editor的話,focusable要爲true。 優化
/** * 初始化rightPopupWindow */ private PopupWindow initPopupWindow() { LayoutInflater inflater = LayoutInflater.from(this); View popView = inflater.inflate(R.layout.popupwindow_content, null); TextView item = (TextView) popView.findViewById(R.id.text_item); // 建立PopupWindow final PopupWindow popupWindow = createPopupWindow(popView); popView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (rightPop != null && rightPop.isShowing()) { popupWindow.dismiss(); } return true; } }); item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); // ... } }); return popupWindow; }
/** * 建立PopupWindow * * @param popView * 指定PopupWindow的內容 */ private PopupWindow createPopupWindow(View popView) { PopupWindow popupInstance = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); popupInstance.setFocusable(true); popupInstance.setBackgroundDrawable(new ColorDrawable(0)); popupInstance.setOutsideTouchable(true); // 監聽器 popupInstance.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // ... } }); return popupInstance; }
例如一個button,點擊該按鈕,顯示popupWindow,而且要求popupWindow顯示在該按鈕下面,則 動畫
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupWindow popupWindow = ...; popupWindow.showAsDropDown(v); } });
另外,還支持指定位+置偏移量顯示: this
public void showAsDropDown(View anchor, int xoff, int yoff);
public void showAtLocation (View parent, int gravity, int x, int y) Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP. Parameters parent a parent view to get the getWindowToken() token from gravity the gravity which controls the placement of the popup window x the popup's x location offset y the popup's y location offset
例如顯示的時候,從底部出現或者從頂部出現等,能夠添加自定義動畫效果。 spa
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="250" android:fromYDelta="100.0%" android:toYDelta="0.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="250" android:fromYDelta="0.0" android:toYDelta="100%" /> </set>
默認的popupWindow顯示的時候,是沒有像Dialog那樣的背景變成灰色半透明效果的。能夠本身添加相似Dialog或ios那樣的效果。 code
例如,設置popupWindow的顯示內容的佈局文件的background屬性爲灰色半透明顏色,而後popupWindow在建立的時候指定寬、高屬性爲MATCH_PARENT便可實現除了popupWindow之外區域變成灰色半透明效果。
固然,這個灰色區域,其實也屬於popupWindow,只是沒有真正的內容。這時候若是須要設置點擊popupWindow之外區域讓它自動消失的話,就須要額外處理。由於點擊灰色部分,你仍然點擊的是popupWindow自己。
一種方法是給popupWindow的整個佈局區域添加onTouch事件監聽器,手動讓popupWindow去dismiss()掉。