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()一下就好了。