自定义PopupWindow
package com.example.zhh.myapplicationdialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
/**
* Android7.0上PopupWindow的showAsDropDown位置问题
* 自定义PopupWindow
* 重写showAsDropDown方法
* 注意构造方法的写法
*/
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);
}
}