android - popupWindow.setElevation()怎樣兼容到API21以下呢
問題描述
項目里有個地方用到了PopupWindow的setElevation() 但是這個方法只支持API21以上,我看有個PopupWindowCompat,但是里面好像沒有什么有用的方法,請問應(yīng)該如何兼容API21以下的版本呢
問題解答
回答1:正常方法是沒有的, 但是通過Java反射還是能實現(xiàn)的, 只是實現(xiàn)了, 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, 然后調(diào)用ViewCompat.setElevation(View, float)就算完成工作了. 但是由于低版本是沒有elevation屬性的, 因此, 上述調(diào)用都只是做了兼容處理保證不崩潰而已.
另外一種思路:
高版本通過設(shè)置elevation
低版本通過Background Drawable
p.s.為什么不干脆都用Background Drawable
相關(guān)文章:
1. bootstrp是col-md-12列的,只有col-md-10有內(nèi)容,可以讓沒有內(nèi)容的不占據(jù)位置嗎;2. wordpress里,這樣的目錄列表是屬于小工具還是啥?3. 常量在外面不加引號會報錯。4. mysql federated引擎無法開啟5. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI6. sublime text3安裝package control失敗7. mysql 為什么主鍵 id 和 pid 都市索引, id > 10 走索引 time > 10 不走索引?8. python如何設(shè)置一個隨著系統(tǒng)時間變化的動態(tài)變量?9. python 3.4 error: Microsoft Visual C++ 10.0 is required10. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null
