Android Popwindow用法
1.基本使用方法
View view = getLayoutInflater().inflate(R.layout.activity_photo_preview, null); ...... if (popupBigPhoto == null) { popupBigPhoto = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); popupBigPhoto.setOutsideTouchable(true); popupBigPhoto.setOnDismissListener(this); } if (popupBigPhoto.isShowing()) { popupBigPhoto.dismiss(); } else { popupBigPhoto.showAtLocation(headview, Gravity.TOP, 0, 0); }
2.屬性方法
1.基本屬性方法
// 設定PopupWindow的背景 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 設定PopupWindow是否能響應外部點選事件 window.setOutsideTouchable(true); // 設定PopupWindow是否能響應點選事件 window.setTouchable(true);
2.在彈窗出現後讓背景變暗,並在彈窗消失後讓背景還原
window.setOnDismissListener(new PopupWindow.OnDismissListener(){ @Override public void onDismiss() { WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=1.0f; getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHND); getWindow().setAttributes(lp); } }); window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0); WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().setAttributes(lp);
3.新增動畫
自定義一個動畫
<!-- res/values/styles.xml --> <style name="animTranslate"> <item name="android:windowEnterAnimation">@anim/translate_in</item> <item name="android:windowExitAnimation">@anim/translate_out</item> </style>
新增動畫
window.setAnimationStyle(R.style.animTranslate);
3.位置設定
- 相對於父佈局的位置
public void showAtLocation(View parent, int gravity, int x, int y) 第二個引數gravity指的是popupWindow在父佈局中出現的大致位置。常見的有 Gravity.NO_GRAVITY,Gravity.LEFT,Gravity.RIGHT,Gravity.TOP,Gravity.BOTTOM。 第三個引數int x指的是以第二個引數gravity指點的位置為原點,popupWindow相對於原點X軸上的位置。x為正popupWindow向右移動,x為負popupWindow向左移動。 第四個引數int y同X差不多,指的是y軸上的位置。y為正popupWindow向上,y為負popupWindow向下。
- 相對於某個控制元件的位置
public void showAsDropDown(View anchor) public void showAsDropDown(View anchor, int xoff, int yoff) public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) 前兩個方法不指定gravity 則popupWindow出現在anchor的正下方。 第一個引數anchor指的是你的popupWindow相對於的這個控制元件。 第二個引數xoff指的是popupWindow相對於原點X軸上的位置。x為正popupWindow向右移動,x為負popupWindow向左移動。 第三個引數yoff指的是popupWindow相對於原點y軸上的位置。y為正popupWindow向下,y為負popupWindow向上。
4.popwindow被軟鍵盤遮擋實現方式
private void showPop(View view) { if (popWindow != null && imms != null) { imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); } if (popWindow == null) { imms = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); View layout = LayoutInflater.from(this).inflate(R.layout.live_qa_saysth, null); ...... popWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); popWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000)); popWindow.setOutsideTouchable(true); } if (!popWindow.isShowing()) { popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0); } else { popWindow.dismiss(); } }
注意點
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); window.setOutsideTouchable(true);
只有同時設定PopupWindow的背景和可以響應外部點選事件,它才能“真正”響應外部點選事件。也就是說,當你點選PopupWindow的外部或者按下“Back”鍵時,PopupWindow才會消失。
特殊情況處理:
1.在popwindow中巢狀viewpager時候,關於定位問題:首先保證viewpager類是同一個,就是沒有新new一個類。然後在show的時候記得setCurrentItem()一下就好了。
參考資料
ofollow,noindex">Android PopupWindow使用方法小結
喵印~~