好久沒寫博客了,記錄下今天遇到的問題。showAsDropDown在7.0後需要適配。
造成此問題的原因是 華爲等全面屏在
relayView.getResources().getDisplayMetrics()
獲取高度的時候,把通知欄的高度也囊括進來了,找了一天,網上的代碼比較過時了,使用後都沒什麼效果。
修改方案(獲取activity的高度後減去控件高度即可):
if (Build.VERSION.SDK_INT < 24) { popupWindow.showAsDropDown(relayView); } else { Rect rect = new Rect(); relayView.getWindowVisibleDisplayFrame(rect); Activity activity = (Activity) relayView.getContext(); Rect outRectReal = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRectReal ); int height = outRectReal .height() - rect.bottom; popupWindow.setHeight(height ); popupWindow.showAsDropDown(relayView); }
在使用showAsDropDown的時候 替換上邊代碼,即可修復。