問題原因
PopupWindow 的height 使用 match_parent 或 fill_parent導(dǎo)致的問題
問題描述
api >=24(Android 7.0)時 View anchor 相對于anchor 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)
public void showAtLocation(View parent, int gravity, int x, int y)
代碼如下:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(button);
}
});
效果如下
解決方法(網(wǎng)上有很多方法)
if (Build.VERSION.SDK_INT < 24){
popupWindow.showAsDropDown(button);
} else { // 適配 android 7.0
int[] location = new int[2];
button.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
popupWindow.showAtLocation(button, Gravity.NO_GRAVITY, x,y+button.getHeight());
}
以上代碼解決全屏問題
我擦 android 7.0 問題解決了刊橘,google android 7.1 出來了 MB 問題又來了
最后解決辦法
if (Build.VERSION.SDK_INT < 24) {
popupWindow.showAsDropDown(button);
} else {
int[] location = new int[2]; // 獲取控件在屏幕的位置
button.getLocationOnScreen(location);
if (Build.VERSION.SDK_INT == 25) {
int tempheight = popupWindow.getHeight();
if (tempheight == WindowManager.LayoutParams.MATCH_PARENT || screenHeight <= tempheight) {
popupWindow.setHeight(screenHeight - location[1] - button.getHeight());
}
}
popupWindow.showAtLocation(button, Gravity.NO_GRAVITY, location[0], location[1] + button.getHeight());
}
github
大家有什么解決辦法陋率?桩了?蠢棱?