Skip to content

Commit

Permalink
修复在没有NaviBar的手机上PopupWindow位置错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiz committed Oct 31, 2016
1 parent f97f926 commit 4f72ea6
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions library/src/main/java/com/cpiz/android/bubbleview/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

/**
* 工具类
Expand Down Expand Up @@ -48,30 +50,37 @@ public static int dp2px(int dp) {
public static int getNavigationBarHeight(View view) {
Activity activity = getActivity(view);
if (activity != null) {
int flags = activity.getWindow().getAttributes().flags;
if ((flags & WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0) { // 表示当前屏幕有显示导航条
// 方法一
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
// DisplayMetrics metrics = new DisplayMetrics();
// defaultDisplay.getMetrics(metrics);
// int usableHeight = metrics.heightPixels;
// defaultDisplay.getRealMetrics(metrics); // getRealMetrics is only available with API 17 and +
// int realHeight = metrics.heightPixels;
// return realHeight > usableHeight ? realHeight - usableHeight : 0;
// }

// 方法二
Resources resources = activity.getResources();
try {
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
// 方法一
{
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int usableHeight = size.y;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealSize(size); // getRealMetrics is only available with API 17 and +
} else {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (Exception e) {
Log.w(TAG, "getNavigationBarHeight: error", e);
}
} catch (Exception ignored) {
Log.w(TAG, "getNavigationBarHeight error", ignored);
}
int realHeight = size.y;
return realHeight > usableHeight ? realHeight - usableHeight : 0;
}

// 方法二:不能确定当前屏幕上是否有NaviBar
// 不能
// Resources resources = activity.getResources();
// try {
// int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
// if (resourceId > 0) {
// return resources.getDimensionPixelSize(resourceId);
// }
// } catch (Exception ignored) {
// Log.w(TAG, "getNavigationBarHeight error", ignored);
// }
}

return 0;
Expand Down

0 comments on commit 4f72ea6

Please sign in to comment.