转载请注明:https://blog.youkuaiyun.com/u012854870/article/details/79941091
问题说明:
我的PopupWindow是用来展示RecycleView的九宫格分类,在Android7.0及以上手机上测试发现showAsDropDown(view)
展示时发现会充满屏幕,而不是展示在view
的下方,测试发现在Android7.0以下都没有类似问题。
问题解决:
自定义PopupWindow重写showAsDropDown(view),如下:
/**
* Created by zorro on 2018/4/14.
* 备注:
*/
public class MyPopupWindow extends PopupWindow {
Context mContext;
private WindowManager mWindowManager;
public MyPopupWindow(View contentView, int width, int height, boolean focusable) {
super();
if (contentView != null) {
mContext = contentView.getContext();
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
setContentView(contentView);
setWidth(width);
setHeight(height);
setFocusable(focusable);
}
@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
}