瀏覽代碼

使用.getLocationInWindow替换.getLocationOnScreen以解决显示对话框在按钮上这个功能在win11安卓子系统上位置是根据电脑显示器的位置定位的bug;

matcha-xiaobin 1 年之前
父節點
當前提交
5a8a119b05
共有 1 個文件被更改,包括 13 次插入7 次删除
  1. 13 7
      DialogX/src/main/java/com/kongzue/dialogx/dialogs/CustomDialog.java

+ 13 - 7
DialogX/src/main/java/com/kongzue/dialogx/dialogs/CustomDialog.java

@@ -4,6 +4,7 @@ import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
 import android.app.Activity;
 import android.graphics.Color;
+import android.util.Log;
 import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
@@ -178,7 +179,7 @@ public class CustomDialog extends BaseDialog {
         public void init() {
             if (baseViewLoc == null && baseView() != null) {
                 baseViewLoc = new int[4];
-                baseView().getLocationOnScreen(baseViewLoc);
+                baseView().getLocationInWindow(baseViewLoc);
                 baseViewLoc[2] = baseView().getWidth();
                 baseViewLoc[3] = baseView().getHeight();
             }
@@ -263,8 +264,8 @@ public class CustomDialog extends BaseDialog {
                     Runnable onLayoutChangeRunnable = new Runnable() {
                         @Override
                         public void run() {
-                            int baseViewLeft = baseViewLoc[0];
-                            int baseViewTop = baseViewLoc[1];
+                            int baseViewLeft = baseViewLoc[0] - (int)boxRoot.getX();
+                            int baseViewTop = baseViewLoc[1] - (int)boxRoot.getY();
                             int calX = 0, calY = 0;
                             if (alignViewGravity != -1) {
                                 if (isAlignBaseViewGravity(Gravity.CENTER_VERTICAL)) {
@@ -300,6 +301,11 @@ public class CustomDialog extends BaseDialog {
 
                                 onGetBaseViewLoc(baseViewLoc);
                             }
+                            Log.d("TAG", "设置" +
+                                    "" +
+                                    " X-> " + calX + "  Y-> " + calY + ",  "+
+                                    " baseViewLeft-> " + baseViewLeft + "  baseViewTop-> " + baseViewTop + ",  "
+                            );
                         }
                     };
 
@@ -309,7 +315,7 @@ public class CustomDialog extends BaseDialog {
                         public void onDraw() {
                             int[] baseViewLocCache = new int[2];
                             if (baseView() != null) {
-                                baseView().getLocationOnScreen(baseViewLocCache);
+                                baseView().getLocationInWindow(baseViewLocCache);
                                 if (getDialogImpl() != null && isShow) {
                                     baseViewLoc[0] = baseViewLocCache[0];
                                     baseViewLoc[1] = baseViewLocCache[1];
@@ -896,7 +902,7 @@ public class CustomDialog extends BaseDialog {
         this.baseView(baseView);
         this.alignViewGravity = alignGravity;
         baseViewLoc = new int[4];
-        baseView.getLocationOnScreen(baseViewLoc);
+        baseView.getLocationInWindow(baseViewLoc);
         setFullScreen(true);
         return this;
     }
@@ -904,7 +910,7 @@ public class CustomDialog extends BaseDialog {
     public CustomDialog setAlignBaseView(View baseView) {
         this.baseView(baseView);
         baseViewLoc = new int[4];
-        baseView.getLocationOnScreen(baseViewLoc);
+        baseView.getLocationInWindow(baseViewLoc);
         setFullScreen(true);
         return this;
     }
@@ -913,7 +919,7 @@ public class CustomDialog extends BaseDialog {
         this.alignViewGravity = alignGravity;
         if (baseView() != null) {
             baseViewLoc = new int[4];
-            baseView().getLocationOnScreen(baseViewLoc);
+            baseView().getLocationInWindow(baseViewLoc);
         }
         setFullScreen(true);
         return this;