Android7.0 popupwindow位置错误解决办法
作为一名开发,同时也是一名搞机人士,某天看到自家产品在Android7.0系统上面的popupwindow位置出毛病了,如下图:
然后看网上说是系统级bug,那就自定义一个popupwindow所有版本通用,如下:
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;
public class FixNPopWindow extends PopupWindow {
@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);
}
}
调用方法如下:
pop = new FixNPopWindow();
pop.setContentView(view);
pop.setHeight(RadioGroup.LayoutParams.MATCH_PARENT);
pop.setWidth(RadioGroup.LayoutParams.MATCH_PARENT);
pop.setFocusable(focusable);
pop.setOutsideTouchable(true);
pop.setTouchable(true);
pop.setBackgroundDrawable(new BitmapDrawable());
//pop.setAnimationStyle(R.style.mypopwindow_anim_style);
pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
pop.showAsDropDown(line);
在4.0、5.0、6.0、7.0上测试没问题。