Browse Source

0.0.45.beta19
- CustomDialog 增加 setAlignBaseViewGravity(...) 相关设置以绑定围绕一个 view 周围显示对话框;
- CustomDialog 增加 setBaseViewMargin(...) 及相关单独设置,以设置围绕 view 周围显示时的间距;
- CustomDialog 增加 setWidth(...) 和 setHeight(...) 相关设置可直接设置对话框内容布局的宽度和高度;
- PopMenu 支持设置空菜单;

kongzue 3 years ago
parent
commit
12739a64af

+ 0 - 17
.idea/deploymentTargetDropDown.xml

@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="deploymentTargetDropDown">
-    <runningDeviceTargetSelectedWithDropDown>
-      <Target>
-        <type value="RUNNING_DEVICE_TARGET" />
-        <deviceKey>
-          <Key>
-            <type value="SERIAL_NUMBER" />
-            <value value="adb-0A201FDD4001T7-5WskUS._adb-tls-connect._tcp" />
-          </Key>
-        </deviceKey>
-      </Target>
-    </runningDeviceTargetSelectedWithDropDown>
-    <timeTargetWasSelectedWithDropDown value="2022-06-14T11:00:41.224396Z" />
-  </component>
-</project>

+ 1 - 0
.idea/misc.xml

@@ -146,6 +146,7 @@
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/fragment_custom.xml" value="0.5411458333333333" />
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/item_custom_recycleview.xml" value="0.5411458333333333" />
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/layout_custom_dialog.xml" value="0.36944444444444446" />
+        <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/layout_custom_dialog_align.xml" value="0.5354166666666667" />
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/layout_custom_dialog_from_top.xml" value="0.5328205128205128" />
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/layout_custom_dialog_t.xml" value="0.5364583333333334" />
         <entry key="..\:/WorkSpace/Android/DialogXDemo/app/src/main/res/layout/layout_custom_recycleview.xml" value="0.5411458333333333" />

+ 229 - 34
DialogX/src/main/java/com/kongzue/dialogx/dialogs/CustomDialog.java

@@ -3,8 +3,10 @@ package com.kongzue.dialogx.dialogs;
 import android.animation.ValueAnimator;
 import android.app.Activity;
 import android.graphics.Color;
+import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.view.animation.DecelerateInterpolator;
@@ -21,6 +23,9 @@ import com.kongzue.dialogx.interfaces.DialogXStyle;
 import com.kongzue.dialogx.interfaces.OnBackPressedListener;
 import com.kongzue.dialogx.interfaces.OnBindView;
 import com.kongzue.dialogx.util.views.DialogXBaseRelativeLayout;
+import com.kongzue.dialogx.util.views.MaxRelativeLayout;
+
+import java.lang.ref.WeakReference;
 
 /**
  * @author: Kongzue
@@ -51,6 +56,13 @@ public class CustomDialog extends BaseDialog {
     protected BOOLEAN privateCancelable;
     protected boolean bkgInterceptTouch = true;
     
+    protected View baseView;
+    protected int alignViewGravity = -1;                                    //指定菜单相对 baseView 的位置
+    protected int width = -1;                                               //指定菜单宽度
+    protected int height = -1;                                              //指定菜单高度
+    protected int[] baseViewLoc;
+    protected int[] marginRelativeBaseView = new int[4];
+    
     public enum ALIGN {
         CENTER,
         TOP,
@@ -111,7 +123,7 @@ public class CustomDialog extends BaseDialog {
     public class DialogImpl implements DialogConvertViewInterface {
         
         public DialogXBaseRelativeLayout boxRoot;
-        public RelativeLayout boxCustom;
+        public MaxRelativeLayout boxCustom;
         
         public DialogImpl(View convertView) {
             if (convertView == null) return;
@@ -148,7 +160,7 @@ public class CustomDialog extends BaseDialog {
                 @Override
                 public boolean onBackPressed() {
                     if (onBackPressedListener != null) {
-                        if (onBackPressedListener.onBackPressed()){
+                        if (onBackPressedListener.onBackPressed()) {
                             dismiss();
                         }
                         return false;
@@ -164,7 +176,9 @@ public class CustomDialog extends BaseDialog {
                 @Override
                 public void run() {
                     Animation enterAnim;
-                    if (enterAnimResId == R.anim.anim_dialogx_default_enter && exitAnimResId == R.anim.anim_dialogx_default_exit) {
+                    if (enterAnimResId == R.anim.anim_dialogx_default_enter &&
+                            exitAnimResId == R.anim.anim_dialogx_default_exit &&
+                            baseView == null) {
                         switch (align) {
                             case TOP:
                                 enterAnimResId = R.anim.anim_dialogx_top_enter;
@@ -228,42 +242,97 @@ public class CustomDialog extends BaseDialog {
             });
         }
         
+        boolean initSetCustomViewLayoutListener = false;
+        
         @Override
         public void refreshView() {
             if (boxRoot == null || getTopActivity() == null) {
                 return;
             }
-            RelativeLayout.LayoutParams rlp;
-            rlp = ((RelativeLayout.LayoutParams) boxCustom.getLayoutParams());
-            if (rlp == null) {
-                rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
-            }
-            switch (align) {
-                case TOP:
-                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
-                    break;
-                case BOTTOM:
-                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
-                    break;
-                case CENTER:
-                    rlp.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
-                    rlp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
-                    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
-                    break;
-                case LEFT:
-                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
-                    break;
-                case RIGHT:
-                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
-                    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
-                    break;
+            if (baseView != null) {
+                if (!initSetCustomViewLayoutListener) {
+                    RelativeLayout.LayoutParams rlp;
+                    rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
+                    boxCustom.setLayoutParams(rlp);
+                    
+                    Runnable onLayoutChangeRunnable = new Runnable() {
+                        @Override
+                        public void run() {
+                            int baseViewLeft = baseViewLoc[0];
+                            int baseViewTop = baseViewLoc[1];
+                            int calX = 0, calY = 0;
+                            if (alignViewGravity != -1) {
+                                if (isAlignBaseViewGravity(Gravity.CENTER_VERTICAL)) {
+                                    calY = (baseViewTop + baseView.getMeasuredHeight() / 2 - boxCustom.getHeight() / 2);
+                                }
+                                if (isAlignBaseViewGravity(Gravity.CENTER_HORIZONTAL)) {
+                                    calX = (baseViewLeft + baseView.getMeasuredWidth() / 2 - boxCustom.getWidth() / 2);
+                                }
+                                if (isAlignBaseViewGravity(Gravity.CENTER)) {
+                                    calX = (baseViewLeft + baseView.getMeasuredWidth() / 2 - boxCustom.getWidth() / 2);
+                                    calY = (baseViewTop + baseView.getMeasuredHeight() / 2 - boxCustom.getHeight() / 2);
+                                }
+                                
+                                if (isAlignBaseViewGravity(Gravity.TOP)) {
+                                    calY = baseViewTop - boxCustom.getHeight() - marginRelativeBaseView[3];
+                                }
+                                if (isAlignBaseViewGravity(Gravity.LEFT)) {
+                                    calX = baseViewLeft - boxCustom.getWidth() - marginRelativeBaseView[2];
+                                }
+                                if (isAlignBaseViewGravity(Gravity.RIGHT)) {
+                                    calX = baseViewLeft + baseView.getWidth() + marginRelativeBaseView[0];
+                                }
+                                if (isAlignBaseViewGravity(Gravity.BOTTOM)) {
+                                    calY = baseViewTop + baseView.getHeight() + marginRelativeBaseView[1];
+                                }
+                                
+                                if (calX != 0) boxCustom.setX(calX);
+                                if (calY != 0) boxCustom.setY(calY);
+                            }
+                        }
+                    };
+                    
+                    boxCustom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
+                        @Override
+                        public void onGlobalLayout() {
+                            onLayoutChangeRunnable.run();
+                        }
+                    });
+                    initSetCustomViewLayoutListener = true;
+                }
+            } else {
+                RelativeLayout.LayoutParams rlp;
+                rlp = ((RelativeLayout.LayoutParams) boxCustom.getLayoutParams());
+                if (rlp == null) {
+                    rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
+                }
+                switch (align) {
+                    case TOP:
+                        rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
+                        break;
+                    case BOTTOM:
+                        rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
+                        break;
+                    case CENTER:
+                        rlp.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
+                        rlp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
+                        rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
+                        break;
+                    case LEFT:
+                        rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
+                        break;
+                    case RIGHT:
+                        rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
+                        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
+                        break;
+                }
+                boxCustom.setLayoutParams(rlp);
             }
-            boxCustom.setLayoutParams(rlp);
             
             boxRoot.setAutoUnsafePlacePadding(autoUnsafePlacePadding);
             if (bkgInterceptTouch) {
@@ -277,13 +346,23 @@ public class CustomDialog extends BaseDialog {
                 } else {
                     boxRoot.setOnClickListener(null);
                 }
-            }else{
+            } else {
                 boxRoot.setClickable(false);
             }
             
             if (onBindView != null && onBindView.getCustomView() != null) {
                 onBindView.bindParent(boxCustom, me);
             }
+            
+            if (width != -1) {
+                boxCustom.setMaxWidth(width);
+                boxCustom.setMinimumWidth(width);
+            }
+            
+            if (height != -1) {
+                boxCustom.setMaxHeight(height);
+                boxCustom.setMinimumHeight(height);
+            }
         }
         
         @Override
@@ -563,4 +642,120 @@ public class CustomDialog extends BaseDialog {
         this.bkgInterceptTouch = bkgInterceptTouch;
         return this;
     }
+    
+    public int getAlignBaseViewGravity() {
+        return alignViewGravity;
+    }
+    
+    /**
+     * 判断是否有设置对应的位置关系
+     *
+     * @param gravity 位置关系
+     * @return 是否具备位置关系
+     */
+    public boolean isAlignBaseViewGravity(int gravity) {
+        return (alignViewGravity & gravity) == gravity;
+    }
+    
+    public CustomDialog setAlignBaseViewGravity(View baseView, int alignGravity) {
+        this.baseView = baseView;
+        this.alignViewGravity = alignGravity;
+        baseViewLoc = new int[2];
+        baseView.getLocationOnScreen(baseViewLoc);
+        setFullScreen(true);
+        return this;
+    }
+    
+    public CustomDialog setAlignBaseViewGravity(View baseView, int alignGravity, int marginLeft,
+                                                int marginTop, int marginRight, int marginBottom) {
+        this.marginRelativeBaseView = new int[]{marginLeft, marginTop, marginRight, marginBottom};
+        return setAlignBaseViewGravity(baseView, alignGravity);
+    }
+    
+    public int[] getBaseViewMargin() {
+        return marginRelativeBaseView;
+    }
+    
+    public CustomDialog setBaseViewMargin(int[] marginRelativeBaseView) {
+        this.marginRelativeBaseView = marginRelativeBaseView;
+        return this;
+    }
+    
+    public CustomDialog setBaseViewMargin(int marginLeft, int marginTop,
+                                                  int marginRight, int marginBottom) {
+        this.marginRelativeBaseView = new int[]{marginLeft, marginTop, marginRight, marginBottom};
+        return this;
+    }
+    
+    public CustomDialog setBaseViewMarginLeft(int marginLeft){
+        this.marginRelativeBaseView[0] = marginLeft;
+        return this;
+    }
+    
+    public CustomDialog setBaseViewMarginTop(int marginTop){
+        this.marginRelativeBaseView[1] = marginTop;
+        return this;
+    }
+    
+    public CustomDialog setBaseViewMarginRight(int marginRight){
+        this.marginRelativeBaseView[2] = marginRight;
+        return this;
+    }
+    
+    public CustomDialog setBaseViewMarginBottom(int marginBottom){
+        this.marginRelativeBaseView[3] = marginBottom;
+        return this;
+    }
+    
+    public int getBaseViewMarginLeft(int marginLeft){
+        return this.marginRelativeBaseView[0];
+    }
+    
+    public int getBaseViewMarginTop(int marginLeft){
+        return this.marginRelativeBaseView[1];
+    }
+    
+    public int getBaseViewMarginRight(int marginLeft){
+        return this.marginRelativeBaseView[2];
+    }
+    
+    public int getBaseViewMarginBottom(int marginLeft){
+        return this.marginRelativeBaseView[3];
+    }
+    
+    public View getBaseView() {
+        return baseView;
+    }
+    
+    public int getWidth() {
+        return width;
+    }
+    
+    /**
+     * 设置菜单 UI 宽度(单位:像素)
+     *
+     * @param width 宽度(像素)
+     * @return PopMenu实例
+     */
+    public CustomDialog setWidth(int width) {
+        this.width = width;
+        refreshUI();
+        return this;
+    }
+    
+    public int getHeight() {
+        return height;
+    }
+    
+    /**
+     * 设置菜单 UI 高度(单位:像素)
+     *
+     * @param height 高度(像素)
+     * @return PopMenu实例
+     */
+    public CustomDialog setHeight(int height) {
+        this.height = height;
+        refreshUI();
+        return this;
+    }
 }

+ 0 - 1
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -785,7 +785,6 @@ public class PopMenu extends BaseDialog {
         return this;
     }
     
-    
     public PopMenu setCustomView(OnBindView<PopMenu> onBindView) {
         this.onBindView = onBindView;
         refreshUI();

+ 4 - 0
DialogX/src/main/java/com/kongzue/dialogx/util/PopMenuArrayAdapter.java

@@ -17,6 +17,7 @@ import android.widget.TextView;
 import com.kongzue.dialogx.R;
 import com.kongzue.dialogx.dialogs.PopMenu;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -42,6 +43,9 @@ public class PopMenuArrayAdapter extends BaseAdapter {
     
     @Override
     public int getCount() {
+        if (menuList == null) {
+            menuList = new ArrayList<>();
+        }
         return menuList.size();
     }
     

+ 1 - 1
DialogX/src/main/res/layout/layout_dialogx_custom.xml

@@ -11,7 +11,7 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent">
 
-        <RelativeLayout
+        <com.kongzue.dialogx.util.views.MaxRelativeLayout
             android:id="@+id/box_custom"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

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

@@ -6,6 +6,7 @@ import android.content.Intent;
 import android.content.res.Configuration;
 import android.graphics.Color;
 import android.net.Uri;
+import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
@@ -116,6 +117,7 @@ public class MainActivity extends BaseActivity {
     private TextView btnCustomInputDialog;
     private TextView btnCustomBottomMenu;
     private TextView btnCustomDialog;
+    private TextView btnCustomDialogAlign;
     private TextView btnFullScreenDialogWebPage;
     private TextView btnFullScreenDialogLogin;
     private TextView btnFullScreenDialogFragment;
@@ -166,6 +168,7 @@ public class MainActivity extends BaseActivity {
         btnCustomInputDialog = findViewById(R.id.btn_customInputDialog);
         btnCustomBottomMenu = findViewById(R.id.btn_customBottomMenu);
         btnCustomDialog = findViewById(R.id.btn_customDialog);
+        btnCustomDialogAlign = findViewById(R.id.btn_customDialogAlign);
         btnFullScreenDialogWebPage = findViewById(R.id.btn_fullScreenDialog_webPage);
         btnFullScreenDialogLogin = findViewById(R.id.btn_fullScreenDialog_login);
         btnFullScreenDialogFragment = findViewById(R.id.btn_fullScreenDialog_fragment);
@@ -812,6 +815,35 @@ public class MainActivity extends BaseActivity {
             }
         });
         
+        btnCustomDialogAlign.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                CustomDialog.show(new OnBindView<CustomDialog>(R.layout.layout_custom_dialog_align) {
+            
+                            private TextView btnSelectPositive;
+                            
+                            @Override
+                            public void onBind(final CustomDialog dialog, View v) {
+                                btnSelectPositive = v.findViewById(R.id.btn_selectPositive);
+                                btnSelectPositive.setOnClickListener(new View.OnClickListener() {
+                                    @Override
+                                    public void onClick(View v) {
+                                        PopTip.show("我知道了");
+                                        dialog.dismiss();
+                                    }
+                                });
+                            }
+                        })
+                        .setCancelable(false)
+                        .setMaskColor(getResources().getColor(R.color.black30))
+                        .setEnterAnimResId(R.anim.anim_custom_pop_enter)
+                        .setExitAnimResId(R.anim.anim_custom_pop_exit)
+                        .setAlignBaseViewGravity(btnCustomDialogAlign, Gravity.TOP)
+                        .setBaseViewMarginBottom(-dip2px(45))
+                        .show();
+            }
+        });
+        
         btnPoptip.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

+ 17 - 0
app/src/main/res/anim/anim_custom_pop_enter.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <scale
+        android:duration="300"
+        android:fromXScale="0.9"
+        android:fromYScale="1.1"
+        android:pivotX="50%"
+        android:pivotY="50%"
+        android:toXScale="1.0"
+        android:toYScale="1.0" />
+
+    <alpha
+        android:duration="300"
+        android:fromAlpha="0.0"
+        android:toAlpha="1.0" />
+</set>

+ 9 - 0
app/src/main/res/anim/anim_custom_pop_exit.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<set xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <alpha
+        android:duration="300"
+        android:fromAlpha="1.0"
+        android:toAlpha="0.0" />
+
+</set>

+ 15 - 0
app/src/main/res/layout/activity_main.xml

@@ -672,6 +672,21 @@
                 android:textSize="13dp"
                 android:textStyle="bold" />
 
+            <TextView
+                android:id="@+id/btn_customDialogAlign"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_margin="5dp"
+                android:background="@drawable/rect_button"
+                android:paddingLeft="15dp"
+                android:paddingTop="10dp"
+                android:paddingRight="15dp"
+                android:paddingBottom="10dp"
+                android:text="对话框显示在按钮上"
+                android:textColor="@color/white"
+                android:textSize="13dp"
+                android:textStyle="bold" />
+
             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"

+ 0 - 1
app/src/main/res/layout/layout_custom_dialog.xml

@@ -1,7 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_gravity="center_vertical"
     android:layout_height="300dp">
 
     <ImageView

+ 29 - 0
app/src/main/res/layout/layout_custom_dialog_align.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content">
+
+    <ImageView
+        android:id="@+id/img_bkg"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:src="@mipmap/img_custom_align_dialog_bkg"/>
+
+    <TextView
+        android:id="@+id/btn_selectPositive"
+        android:layout_width="wrap_content"
+        android:layout_height="36dp"
+        android:background="@drawable/button_dialogx_material_light"
+        android:clickable="true"
+        android:gravity="center"
+        android:paddingLeft="15dp"
+        android:paddingRight="15dp"
+        android:text="我知道了"
+        android:textStyle="bold"
+        android:layout_alignBottom="@+id/img_bkg"
+        android:layout_centerHorizontal="true"
+        android:layout_marginBottom="65dp"
+        android:textColor="@color/colorAccent"
+        android:textSize="15dp" />
+
+</RelativeLayout>

BIN
app/src/main/res/mipmap-xxhdpi/img_custom_align_dialog_bkg.png


+ 1 - 1
gradle.properties

@@ -19,5 +19,5 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.45.beta18
+BUILD_VERSION=0.0.45.beta19
 BUILD_VERSION_INT=44