android 华为 showAsDropDown位置错乱

华为7.0会出现位置错乱,网上有两种方法。

第一种:使用showAtLocation方法

if (Build.VERSION.SDK_INT != 24) { 
    brandPopup.showAsDropDown(parent); 
} else { // 获取控件的位置,安卓系统>7.0 
    int[] location = new int[2]; 
    parent.getLocationOnScreen(location); 
    brandPopup.showAtLocation(parent, Gravity.NO_GRAVITY, location[0], location[1] + parent.getHeight()); 
}

第二种:

popwindow重写showAsDropDown方法。

 @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
发布了14 篇原创文章 · 获赞 23 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_27378951/article/details/88837688