您的位置:首页 > 运维架构

日常工作记录:解决7.0系统PopupWindow.showAsDropDown()显示位置不正确问题

2018-04-11 15:56 2106 查看
今天做项目的时候发现,安卓5.0机子的PopupWindow显示位置正确,但放在7.0系统的机子上确错位,经百度了一下发现到解决问题(特此以作记录),废话不说直接重写PopupWindow的showAsDropDown();方法即可,下面贴上方法
package com.shixia.sxhr.widget;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

/**
* Created by Administrator on 2017/11/16.
* 解决7.0 popuwindow 显示位置不正确问题
*/

public class MyPopupWindow extends PopupWindow {

public MyPopupWindow(Context context) {
super(context);
}

public MyPopupWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public MyPopupWindow() {
}

public MyPopupWindow(View contentView, int width, int height) {
super(contentView, width, height);
}

@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect visibleFrame = new Rect();
anchor.getGlobalVisibleRect(visibleFrame);
int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
setHeight(height);
}
super.showAsDropDown(anchor);
}

@Override
public void showAsDropDown(View anchorView, int xoff, int yoff) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
int[] a = new int[2];
anchorView.getLocationInWindow(a);
showAtLocation(anchorView, Gravity.NO_GRAVITY, xoff, a[1] + anchorView.getHeight() + yoff);
} else {
super.showAsDropDown(anchorView, xoff, yoff);
}
}

}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: