您的位置:首頁技術文章
文章詳情頁

android - popupWindow.setElevation()怎樣兼容到API21以下呢

瀏覽:112日期:2024-08-20 08:04:25

問題描述

項目里有個地方用到了PopupWindow的setElevation() 但是這個方法只支持API21以上,我看有個PopupWindowCompat,但是里面好像沒有什么有用的方法,請問應該如何兼容API21以下的版本呢

android - popupWindow.setElevation()怎樣兼容到API21以下呢

問題解答

回答1:

正常方法是沒有的, 但是通過Java反射還是能實現的, 只是實現了, elevation的效果也是出不來的. 先看看Android源碼:

public class PopupWindow { ... ... private View mBackgroundView; public void setElevation(float elevation) {mElevation = elevation; } private void preparePopup(WindowManager.LayoutParams p) {... ...// The background owner should be elevated so that it casts a shadow.mBackgroundView.setElevation(mElevation); }}

從上面的代碼, 可以知道只要通過反射獲取到mBackgroundView, 然后調用ViewCompat.setElevation(View, float)就算完成工作了. 但是由于低版本是沒有elevation屬性的, 因此, 上述調用都只是做了兼容處理保證不崩潰而已.

另外一種思路:

高版本通過設置elevation

低版本通過Background Drawable

p.s.為什么不干脆都用Background Drawable

相關文章:
国产综合久久一区二区三区