7.0锚点位置错乱现象
重写PopupWindow showAsDropDown方法;
public class CustomerPopupWindow extends PopupWindow {
private Context mContext;
private Drawable mBackgroundDrawable;
public CustomerPopupWindow(Context context) {
this.mContext = context;
}
public CustomerPopupWindow(View contentView, int width, int height) {
super(contentView, width, height);
}
@Override
public void showAsDropDown(View anchor) {
//解决7.0锚点位置错乱现象
if (Build.VERSION.SDK_INT >= 24) {
Rect visibleFrame = new Rect();
anchor.getGlobalVisibleRect(visibleFrame);
int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
setHeight(height);
}
super.showAsDropDown(anchor);
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
if (Build.VERSION.SDK_INT >= 24) {
Rect visibleFrame = new Rect();
anchor.getGlobalVisibleRect(visibleFrame);
int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
setHeight(height);
}
super.showAsDropDown(anchor, xoff, yoff);
}
/**
* 设置背景色
*
* @param bgAlpha
* @param window
*/
public void backgroundAlpha(float bgAlpha, Window window) {
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
window.setAttributes(lp);
}
}
点击显示按钮无法隐藏
View contentView = LayoutInflater.from(this).inflate(R.layout.popu_wimdow_withdrawdeposit, null);
popupWindow = new CustomerPopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.showPopupAnimation);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//主要下面两个设置
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
popupWindow.dismiss();
return true;
}
return false;
}
});
和软键盘冲突
有EditText的界面时先判断 软键盘 是否显示 显示则隐藏
daima
//软键盘显示则隐藏
public void hideInputWindow(Activity context) {
if (context == null) {
return;
}
final View v = ((Activity) context).getWindow().peekDecorView();
if (v != null && v.getWindowToken() != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}