项目里有个地方用到了PopupWindow
的setElevation()
但是这个方法只支持API21以上,我看有个PopupWindowCompat
,但是里面好像没有什么有用的方法,请问应该如何兼容API21以下的版本呢
怪我咯2017-04-18 09:16:05
There is no normal method, but it can still be achieved through Java reflection. However, even if it is implemented, the effect of elevation will not be produced. Take a look at the Android source code first:
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);
}
}
From the above code, we can know that as long as mBackgroundView
, 然后调用ViewCompat.setElevation(View, float)
is obtained through reflection, the work is completed. However, since the lower version does not have the elevation attribute, the above calls are only processed for compatibility to ensure that they do not crash.
Another way of thinking:
High version by setting elevation
lower version passedBackground Drawable
p.s. Why not just use them allBackground Drawable