android - popupWindow.setElevation()怎样兼容到API21以下呢
黄舟
黄舟 2017-04-18 09:14:05
0
1
618

项目里有个地方用到了PopupWindowsetElevation() 但是这个方法只支持API21以上,我看有个PopupWindowCompat,但是里面好像没有什么有用的方法,请问应该如何兼容API21以下的版本呢

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
刘奇

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:

  1. High version by setting elevation

  2. lower version passedBackground Drawable

p.s. Why not just use them allBackground Drawable

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template