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.基本屬性方法
// 設(shè)置PopupWindow的背景
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 設(shè)置PopupWindow是否能響應(yīng)外部點擊事件
window.setOutsideTouchable(true);
// 設(shè)置PopupWindow是否能響應(yīng)點擊事件
window.setTouchable(true);
2.在彈窗出現(xiàn)后讓背景變暗,并在彈窗消失后讓背景還原
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.位置設(shè)置
- 相對于父布局的位置
public void showAtLocation(View parent, int gravity, int x, int y)
第二個參數(shù)gravity指的是popupWindow在父布局中出現(xiàn)的大致位置怠李。常見的有 Gravity.NO_GRAVITY娶聘,Gravity.LEFT,Gravity.RIGHT,Gravity.TOP,Gravity.BOTTOM抑诸。
第三個參數(shù)int x指的是以第二個參數(shù)gravity指點的位置為原點吹散,popupWindow相對于原點X軸上的位置台谊。x為正popupWindow向右移動蓉媳,x為負(fù)popupWindow向左移動。
第四個參數(shù)int y同X差不多锅铅,指的是y軸上的位置酪呻。y為正popupWindow向上,y為負(fù)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出現(xiàn)在anchor的正下方玩荠。
第一個參數(shù)anchor指的是你的popupWindow相對于的這個控件。
第二個參數(shù)xoff指的是popupWindow相對于原點X軸上的位置贼邓。x為正popupWindow向右移動阶冈,x為負(fù)popupWindow向左移動。
第三個參數(shù)yoff指的是popupWindow相對于原點y軸上的位置塑径。y為正popupWindow向下女坑,y為負(fù)popupWindow向上。
4.popwindow被軟鍵盤遮擋實現(xiàn)方式
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);
只有同時設(shè)置PopupWindow的背景和可以響應(yīng)外部點擊事件统舀,它才能“真正”響應(yīng)外部點擊事件匆骗。也就是說劳景,當(dāng)你點擊PopupWindow的外部或者按下“Back”鍵時,PopupWindow才會消失碉就。
特殊情況處理:
1.在popwindow中嵌套viewpager時候盟广,關(guān)于定位問題:首先保證viewpager類是同一個,就是沒有新new一個類铝噩。然后在show的時候記得setCurrentItem()一下就好了衡蚂。
參考資料
Android PopupWindow使用方法小結(jié)
Android中文API——PopupWindow
喵印~~