Răsfoiți Sursa

Merge pull request #381 from Matcha-xiaobin/master

尝试修复 #370 bug
Kongzue 1 an în urmă
părinte
comite
ddda693f88

+ 32 - 11
DialogX/src/main/java/com/kongzue/dialogx/util/views/DialogXBaseRelativeLayout.java

@@ -17,20 +17,17 @@ import android.view.inputmethod.InputMethodManager;
 import android.widget.RelativeLayout;
 
 import androidx.annotation.Px;
+import androidx.core.graphics.Insets;
+import androidx.core.view.WindowInsetsCompat;
 
 import com.kongzue.dialogx.DialogX;
 import com.kongzue.dialogx.R;
 import com.kongzue.dialogx.interfaces.BaseDialog;
 import com.kongzue.dialogx.interfaces.DialogXBaseBottomDialog;
-import com.kongzue.dialogx.interfaces.DynamicWindowInsetsAnimationListener;
 import com.kongzue.dialogx.interfaces.NoTouchInterface;
 import com.kongzue.dialogx.interfaces.OnSafeInsetsChangeListener;
 
 import java.lang.ref.WeakReference;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 /**
  * @author: Kongzue
@@ -113,16 +110,40 @@ public class DialogXBaseRelativeLayout extends RelativeLayout {
                     if (unsafePlace == null) {
                         unsafePlace = new Rect();
                     }
-                    unsafePlace.left = start;
-                    unsafePlace.top = top;
-                    unsafePlace.right = end;
-                    unsafePlace.bottom = bottom;
+                    // TODO Fix bug #370 在这里对这个重新做下处理,未详细测试,比如键盘弹起时的情况
+                    Insets systemBarInsets = null;
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+                        WindowInsetsCompat windowInsetsCompat = WindowInsetsCompat.toWindowInsetsCompat(getRootWindowInsets());
+                        boolean navigationBarsVisible = windowInsetsCompat.isVisible(WindowInsetsCompat.Type.navigationBars());
+                        boolean imeVisible = windowInsetsCompat.isVisible(WindowInsetsCompat.Type.ime());
+                        if (!imeVisible && navigationBarsVisible) {
+                            systemBarInsets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.systemBars());
+                            if (systemBarInsets.bottom == bottom &&
+                                    systemBarInsets.top == top &&
+                                    systemBarInsets.left == start &&
+                                    systemBarInsets.right == end
+                            ) {
+                                systemBarInsets = null;
+                            }
+                        }
+                    }
+                    if (systemBarInsets != null) {
+                        unsafePlace.left = systemBarInsets.left;
+                        unsafePlace.top = systemBarInsets.top;
+                        unsafePlace.right = systemBarInsets.right;
+                        unsafePlace.bottom = systemBarInsets.bottom;
+                    } else {
+                        unsafePlace.left = start;
+                        unsafePlace.top = top;
+                        unsafePlace.right = end;
+                        unsafePlace.bottom = bottom;
+                    }
 
                     if (onSafeInsetsChangeListener != null) {
                         onSafeInsetsChangeListener.onChange(unsafePlace);
                     }
 
-                    setUnsafePadding(start, top, end, bottom);
+                    setUnsafePadding(unsafePlace.left, unsafePlace.top, unsafePlace.right, unsafePlace.bottom);
                 }
 
                 @Override
@@ -144,7 +165,7 @@ public class DialogXBaseRelativeLayout extends RelativeLayout {
     }
 
     public void setUnsafePadding(@Px int start, @Px int top, @Px int end, @Px int bottom) {
-        log("KONGZUE DEBUG DIALOGX: getParentDialog()=" + getParentDialog()+" t=" + top + " b=" + bottom);
+        log("KONGZUE DEBUG DIALOGX: getParentDialog()=" + getParentDialog() + " t=" + top + " b=" + bottom);
         if (getParentDialog() instanceof DialogXBaseBottomDialog) {
             log("  KONGZUE DEBUG DIALOGX: isDialogXBaseBottomDialog");
             ViewGroup bkgView = findViewById(R.id.bkg);

+ 4 - 1
app/src/main/AndroidManifest.xml

@@ -21,8 +21,11 @@
             android:theme="@style/AppCompatTheme"
             android:windowSoftInputMode="adjustResize" />
 
+<!--            android:name=".activity.MainActivity"-->
+<!--            android:name=".activity.TestMainActivity"-->
+
         <activity
-            android:name=".activity.MainActivity"
+            android:name=".activity.TestMainActivity"
             android:configChanges="orientation|keyboardHidden|screenSize|uiMode|screenLayout|smallestScreenSize|layoutDirection"
             android:exported="true">
             <intent-filter>

+ 32 - 0
app/src/main/java/com/kongzue/dialogxdemo/activity/TestMainActivity.java

@@ -0,0 +1,32 @@
+package com.kongzue.dialogxdemo.activity;
+
+import android.os.Build;
+import android.os.Bundle;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.content.ContextCompat;
+import androidx.core.view.ViewCompat;
+import androidx.core.view.WindowCompat;
+import androidx.core.view.WindowInsetsCompat;
+
+import com.kongzue.dialogx.dialogs.BottomDialog;
+import com.kongzue.dialogxdemo.R;
+
+public class TestMainActivity extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main_test);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.colorAccent));
+        }
+        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
+        findViewById(R.id.btn_showDialog).setOnClickListener(view -> {
+            BottomDialog.show("标题", "这里是对话框内容。")
+                    .setCancelButton("取消", (dialog, v) -> false)
+                    .setOkButton("确定", (dialog, v) -> false);
+        });
+    }
+}

+ 37 - 0
app/src/main/res/layout/activity_main_test.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="#3A3A3A">
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="顶部文本"
+        android:textColor="@color/white"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="底部文本"
+        android:textColor="@color/white"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent" />
+
+    <Button
+        android:id="@+id/btn_showDialog"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Show Dialog!"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+
+</androidx.constraintlayout.widget.ConstraintLayout>