当前位置:   article > 正文

Android-自定义dialog_android 自定义dialog

android 自定义dialog

Android-自定义dialog

在项目中, app没有用原生的Dialog去弹起一个弹窗,在本地new 一个 PhoneWindow去实现dialog的功能,代码如下:

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.internal.policy.PhoneWindow;

@SuppressLint("StaticFieldLeak")
public class MyPopup implements Handler.Callback, XXXXXXXXXControl.OnXXXXXXXXXStatusListener {
    private Handler mHandler = new Handler(this);
    private Context mContext;
    private WindowManager mWindowManager;
    private Window mWindow;
    private ViewGroup mParentLayout;
    private TextView mTitleText;
    private ImageView mIconImg;
    private TextView mMessageText;
    private FrameLayout mCustomLayout;
    private Button mPositiveBtn;
    private Button mNegativeBtn;
    private static final int ACTION_POPUP_DISMISS = 1000;
    private static final long DELAYED_POPUP_DISMISS = 0L;
    private boolean mIsShowing;
    private boolean mCancelable = true;
    private long mAutoCancelDelayMillis;
    private int mWidthType;
    private DialogInterface.OnShowListener mShowListener;
    private DialogInterface.OnDismissListener mDismissListener;

    public MyPopup(Context context) {
        mContext = context;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mWindow = new PhoneWindow(context.getApplicationContext());
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        mWindow.setContentView(R.layout.popup);
        mWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        mWindow.setBackgroundDrawable(new ColorDrawable(0x99000000));
        mWindow.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

        View rootLayout = mWindow.findViewById(R.id.layout_root);
        mParentLayout = mWindow.findViewById(R.id.layout_content);
        mTitleText = mWindow.findViewById(R.id.tv_title);
        mIconImg = mWindow.findViewById(R.id.iv_icon);
        mMessageText = mWindow.findViewById(R.id.tv_message);
        mCustomLayout = mWindow.findViewById(R.id.layout_custom_view);
        mPositiveBtn = mWindow.findViewById(R.id.btn_positive);
        mNegativeBtn = mWindow.findViewById(R.id.btn_negative);

        rootLayout.setOnClickListener(v -> {
            if (mCancelable) {
                hide();
            }
        });
    }

    @Override
    public boolean handleMessage(Message message) {
        if (message.what == ACTION_POPUP_DISMISS) {
            hide();
        }
        return false;
    }

    @Override
    public void onXXXXXXXXXStatusChanged() {
        initPopupAlign();
    }

    private void initPopupAlign() {
        if (!mIsShowing) {
            return;
        }
        boolean isSsShow = isSsShow();
        boolean isCenter = !isSsShow;
        mWindow.setLayout(isCenter ? 1920 : 1280, 720);
        mWindow.setGravity(isCenter ? Gravity.CENTER : Gravity.START);
        if (isCenter) {
            mWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        } else {
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        }
        mWindowManager.updateViewLayout(mWindow.getDecorView(), mWindow.getAttributes());
    }

    private void initPopupWidth() {
        int widthDimensId;
        switch (mWidthType) {
            case AlertDialog.DIALOG_WIDTH_TYPE_SMALL:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_min;
                break;
            case AlertDialog.DIALOG_WIDTH_TYPE_MIDDLE:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_mid;
                break;
            case AlertDialog.DIALOG_WIDTH_TYPE_BIG:
            default:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_max;
                break;
        }
        ViewGroup.LayoutParams layoutParams = mParentLayout.getLayoutParams();
        layoutParams.width = mContext.getResources().getDimensionPixelSize(widthDimensId);
        mParentLayout.setLayoutParams(layoutParams);
    }

    private void removePopupDismissMessage() {
        if (mHandler.hasMessages(ACTION_POPUP_DISMISS)) {
            mHandler.removeMessages(ACTION_POPUP_DISMISS);
        }
    }

    private void initAutoCancelable() {
        if (mAutoCancelDelayMillis != DELAYED_POPUP_DISMISS) {
            removePopupDismissMessage();
            mHandler.sendEmptyMessageDelayed(ACTION_POPUP_DISMISS, mAutoCancelDelayMillis);
        }
    }

    private void requestWindowFocus() {
        if (mParentLayout != null) {
            mParentLayout.setFocusableInTouchMode(true);
            mParentLayout.requestFocus();
        }
    }

    public void show() {
        mWindowManager.addView(mWindow.getDecorView(), mWindow.getAttributes());
        mIsShowing = true;
        notifyPopupShow();
        initPopupAlign();
        initPopupWidth();
        initAutoCancelable();
        requestWindowFocus();
        onShow();
    }

    public void hide() {
        if (!mIsShowing) {
            return;
        }
        mWindowManager.removeView(mWindow.getDecorView());
        mIsShowing = false;
        notifyPopupHide();
        onDismiss();
        removePopupDismissMessage();

        mParentLayout = null;
        mTitleText = null;
        mIconImg = null;
        mMessageText = null;
        mCustomLayout = null;
        mPositiveBtn = null;
        mNegativeBtn = null;
        mWindowManager = null;
        mWindow = null;
        mContext = null;
        mHandler = null;
    }

    public boolean isShowing() {
        return mIsShowing;
    }

    public MyPopup setCancelable(boolean cancelable) {
        mCancelable = cancelable;
        return this;
    }

    public MyPopup setAutoCancelable(long autoCancelDelayMillis) {
        mAutoCancelDelayMillis = autoCancelDelayMillis;
        return this;
    }

    public MyPopup setWidthType(int widthType) {
        mWidthType = widthType;
        return this;
    }

    public MyPopup setTitle(String title) {
        if (mTitleText != null) {
            mTitleText.setVisibility(View.VISIBLE);
            mTitleText.setText(title);
        }
        return this;
    }

    public MyPopup setIconType(int iconType) {
        if (mIconImg != null) {
            mIconImg.setVisibility(View.VISIBLE);
            int iconResId;
            switch (iconType) {
                case AlertDialog.DIALOG_ICON_WARNING:
                    iconResId = com.android.internal.R.drawable.co_warning;
                    break;

                case AlertDialog.DIALOG_ICON_QUESTION:
                    iconResId = com.android.internal.R.drawable.co_question;
                    break;

                default:
                case AlertDialog.DIALOG_ICON_EXCLAMATION:
                case AlertDialog.DIALOG_ICON_DEFAULT:
                    iconResId = com.android.internal.R.drawable.co_info;
                    break;
            }
            mIconImg.setImageResource(iconResId);
        }
        return this;
    }

    public MyPopup setMessage(String message) {
        if (mMessageText != null) {
            mMessageText.setVisibility(View.VISIBLE);
            mMessageText.setText(message);
        }
        return this;
    }

    public MyPopup setCustomView(View view) {
        if (mCustomLayout != null) {
            mCustomLayout.setVisibility(View.VISIBLE);
            mCustomLayout.removeAllViews();
            if (view != null) {
                mCustomLayout.addView(view);
            }
        }
        return this;
    }

    public MyPopup setPositiveButton(String btnText, View.OnClickListener listener) {
        if (mPositiveBtn != null) {
            mPositiveBtn.setVisibility(View.VISIBLE);
            mPositiveBtn.setText(btnText);
            mPositiveBtn.setOnClickListener(v -> {
                hide();
                if (listener != null) {
                    listener.onClick(mPositiveBtn);
                }
            });
        }
        return this;
    }

    public MyPopup setNegativeButton(String btnText, View.OnClickListener listener) {
        if (mNegativeBtn != null) {
            mNegativeBtn.setVisibility(View.VISIBLE);
            mNegativeBtn.setText(btnText);
            mNegativeBtn.setOnClickListener(v -> {
                hide();
                if (listener != null) {
                    listener.onClick(mNegativeBtn);
                }
            });
        }
        return this;
    }

    public MyPopup setOnShowListener(DialogInterface.OnShowListener showListener) {
        mShowListener = showListener;
        return this;
    }

    private void onShow() {
        if (mShowListener != null) {
            mShowListener.onShow(null);
        }
    }

    public MyPopup setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
        mDismissListener = dismissListener;
        return this;
    }

    private void onDismiss() {
        if (mDismissListener != null) {
            mDismissListener.onDismiss(null);
        }
    }

    public MyPopup setOnKeyListener(View.OnKeyListener keyListener) {
        if (mParentLayout != null) {
            mParentLayout.setOnKeyListener(keyListener);
        }
        return this;
    }


    private void notifyPopupShow() {
		// todo ...
    }

    private void notifyPopupHide() {
		// todo ...
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/co_bg_popup"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:background="@drawable/co_bg_popup_title"
            android:gravity="center"
            android:textColor="@color/text_title"
            android:textSize="26sp"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="20dp"
            android:src="@android:drawable/ic_dialog_info"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_title"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/tv_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="30dp"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:textColor="@color/text_title"
            android:textSize="26sp"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@id/btn_positive"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/iv_icon" />

        <FrameLayout
            android:id="@+id/layout_custom_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@id/btn_positive"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_title" />

        <Button
            android:id="@+id/btn_positive"
            android:layout_width="0dp"
            android:layout_height="52dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/co_bt_bottom"
            android:textAllCaps="false"
            android:textColor="@color/popup_button_font_color"
            android:textSize="26sp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/btn_negative"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/btn_negative"
            android:layout_width="0dp"
            android:layout_height="52dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/co_bt_bottom"
            android:textAllCaps="false"
            android:textColor="@color/popup_button_font_color"
            android:textSize="26sp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/btn_positive" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

用上面可以做出Dialog的效果,但是当弹窗起来的时候statusbar不置灰, 因此在构造方法里,向SystemUI这个app里添加一个flag mWindow.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);去通知statusbar去置灰.

目前这个出现问题, 因为两个置灰的时间不一致,导致了在statusbar和弹窗后面的看起不一致,影响用户体验.

修改, 尝试修改,不使用new PhoneWindow(),直接创建一个window,然后将其add上去, 看代码

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManagerImpl;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.internal.policy.PhoneWindow;

@SuppressLint("StaticFieldLeak")
public class MyPopup implements Handler.Callback, XXXXXXXXXControl.OnXXXXXXXXXStatusListener {
    private Handler mHandler = new Handler(this);
    private Context mContext;
    private WindowManager mWindowManager;
    private Window mWindow;
    private ViewGroup mParentLayout;
    private TextView mTitleText;
    private ImageView mIconImg;
    private TextView mMessageText;
    private FrameLayout mCustomLayout;
    private Button mPositiveBtn;
    private Button mNegativeBtn;
    private static final int ACTION_POPUP_DISMISS = 1000;
    private static final long DELAYED_POPUP_DISMISS = 0L;
    private SplitScreenControl mSplitScreenControl;
    private VRObserver mVRObserver;
    private boolean mIsShowing;
    private boolean mCancelable = true;
    private long mAutoCancelDelayMillis;
    private int mWidthType;
    private DialogInterface.OnShowListener mShowListener;
    private DialogInterface.OnDismissListener mDismissListener;

    private View mView;
    private WindowManagerImpl wm;
    private WindowManager.LayoutParams mWindowLayoutParams;

    public MyPopup(Context context) {
        mContext = context;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        mWindow = new PhoneWindow(context.getApplicationContext());
//        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
//        mWindow.setContentView(R.layout.popup);
//        mWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
//        mWindow.setBackgroundDrawable(new ColorDrawable(0x99000000));
//        mWindow.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = (View) inflater.inflate(R.layout.popup, null);



        View rootLayout = mView.findViewById(R.id.layout_root);
        mParentLayout = mView.findViewById(R.id.layout_content);
        mTitleText = mView.findViewById(R.id.tv_title);
        mIconImg = mView.findViewById(R.id.iv_icon);
        mMessageText = mView.findViewById(R.id.tv_message);
        mCustomLayout = mView.findViewById(R.id.layout_custom_view);
        mPositiveBtn = mView.findViewById(R.id.btn_positive);
        mNegativeBtn = mView.findViewById(R.id.btn_negative);

        rootLayout.setOnClickListener(v -> {
            if (mCancelable) {
                hide();
            }
        });
    }

    private void addViewToWindowManager(View view) {
        Log.i("liuhu", "MyTestWindow - addViewToWindowManager");
        if (!mIsShowing) {
            return;
        }
        boolean isSsShow = isSsShow();
        boolean isSsOsdShow = isSsOsdShow();
        boolean isCenter = !isSsShow && !isSsOsdShow;

        wm = (WindowManagerImpl)mContext.getSystemService(Context.WINDOW_SERVICE);
        int flags = 0;
        flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                isCenter ? 1920 : 1280,
                WindowManager.LayoutParams.MATCH_PARENT,
                0,
                0,
                WindowManager.LayoutParams.TYPE_MY_TEST_DIALOG, // 自己选一个层级
                flags,
                PixelFormat.TRANSPARENT);
        lp.setTitle("MyTestWindow");
        lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
        lp.gravity = Gravity.LEFT;
        mWindowLayoutParams = lp;
        wm.addView(view, lp);
    }

    private void changeWindowSize() {
        Log.d("liuhu", "MyTestWindow - changeWindowSize");
        if (!mIsShowing && mView != null) {
            return;
        }
        boolean isSsShow = isSsShow();
        boolean isCenter = !isSsShow;

        mWindowLayoutParams.width = isCenter ? 1920 : 1280;
        wm.updateViewLayout(mView, mWindowLayoutParams);
    }

    private void removeViewToWindowManager(View view) {
        Log.i("liuhu", "MyTestWindow - removeViewToWindowManager");
        wm = (WindowManagerImpl)mContext.getSystemService(Context.WINDOW_SERVICE);
        wm.removeView(view);
    }

    @Override
    public boolean handleMessage(Message message) {
        if (message.what == ACTION_POPUP_DISMISS) {
            hide();
        }
        return false;
    }

    @Override
    public void onXXXXXXXXStatusChanged() {
        changeWindowSize();
    }

    private void initPopupAlign() {
        if (!mIsShowing) {
            return;
        }
        boolean isSsShow = isSsShow();
        boolean isSsOsdShow = isSsOsdShow();
        boolean isCenter = !isSsShow && !isSsOsdShow;
        mWindow.setLayout(isCenter ? 1920 : 1280, 720);
        mWindow.setGravity(isCenter ? Gravity.CENTER : Gravity.START);
        if (isCenter) {
            mWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        } else {
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        }
        mWindowManager.updateViewLayout(mWindow.getDecorView(), mWindow.getAttributes());
    }

    private void initPopupWidth() {
        int widthDimensId;
        switch (mWidthType) {
            case AlertDialog.DIALOG_WIDTH_TYPE_SMALL:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_min;
                break;
            case AlertDialog.DIALOG_WIDTH_TYPE_MIDDLE:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_mid;
                break;
            case AlertDialog.DIALOG_WIDTH_TYPE_BIG:
            default:
                widthDimensId = com.android.internal.R.dimen.alert_dialog_width_max;
                break;
        }
        ViewGroup.LayoutParams layoutParams = mParentLayout.getLayoutParams();
        layoutParams.width = mContext.getResources().getDimensionPixelSize(widthDimensId);
        mParentLayout.setLayoutParams(layoutParams);
    }

    private void removePopupDismissMessage() {
        if (mHandler.hasMessages(ACTION_POPUP_DISMISS)) {
            mHandler.removeMessages(ACTION_POPUP_DISMISS);
        }
    }

    private void initAutoCancelable() {
        if (mAutoCancelDelayMillis != DELAYED_POPUP_DISMISS) {
            removePopupDismissMessage();
            mHandler.sendEmptyMessageDelayed(ACTION_POPUP_DISMISS, mAutoCancelDelayMillis);
        }
    }

    private void requestWindowFocus() {
        if (mParentLayout != null) {
            mParentLayout.setFocusableInTouchMode(true);
            mParentLayout.requestFocus();
        }
    }

    public void show() {
//        mWindowManager.addView(mWindow.getDecorView(), mWindow.getAttributes());
        if (mView!=null) {
            mIsShowing = true;
            notifyPopupShow();
//        initPopupAlign();
            addViewToWindowManager(mView);
            initPopupWidth();
            initAutoCancelable();
            requestWindowFocus();
            onShow();
        }
    }

    public void hide() {
        if (!mIsShowing) {
            return;
        }
//        mWindowManager.removeView(mWindow.getDecorView());
        removeViewToWindowManager(mView);
        mIsShowing = false;
        notifyPopupHide();
        onDismiss();
        removePopupDismissMessage();
        mParentLayout = null;
        mTitleText = null;
        mIconImg = null;
        mMessageText = null;
        mCustomLayout = null;
        mPositiveBtn = null;
        mNegativeBtn = null;
        mWindowManager = null;
        mWindow = null;
        mContext = null;
        mHandler = null;
        mView = null;
        wm = null;
        mWindowLayoutParams = null;
    }

    public boolean isShowing() {
        return mIsShowing;
    }

    public MyPopup setCancelable(boolean cancelable) {
        mCancelable = cancelable;
        return this;
    }

    public MyPopup setAutoCancelable(long autoCancelDelayMillis) {
        mAutoCancelDelayMillis = autoCancelDelayMillis;
        return this;
    }

    public MyPopup setWidthType(int widthType) {
        mWidthType = widthType;
        return this;
    }

    public MyPopup setTitle(String title) {
        if (mTitleText != null) {
            mTitleText.setVisibility(View.VISIBLE);
            mTitleText.setText(title);
        }
        return this;
    }

    public MyPopup setIconType(int iconType) {
        if (mIconImg != null) {
            mIconImg.setVisibility(View.VISIBLE);
            int iconResId;
            switch (iconType) {
                case AlertDialog.DIALOG_ICON_WARNING:
                    iconResId = com.android.internal.R.drawable.co_ic_battery_warning;
                    break;

                case AlertDialog.DIALOG_ICON_QUESTION:
                    iconResId = com.android.internal.R.drawable.co_ic_question;
                    break;

                default:
                case AlertDialog.DIALOG_ICON_EXCLAMATION:
                case AlertDialog.DIALOG_ICON_DEFAULT:
                    iconResId = com.android.internal.R.drawable.co_ic_info;
                    break;
            }
            mIconImg.setImageResource(iconResId);
        }
        return this;
    }

    public MyPopup setMessage(String message) {
        if (mMessageText != null) {
            mMessageText.setVisibility(View.VISIBLE);
            mMessageText.setText(message);
        }
        return this;
    }

    public MyPopup setCustomView(View view) {
        if (mCustomLayout != null) {
            mCustomLayout.setVisibility(View.VISIBLE);
            mCustomLayout.removeAllViews();
            if (view != null) {
                mCustomLayout.addView(view);
            }
        }
        return this;
    }

    public MyPopup setPositiveButton(String btnText, View.OnClickListener listener) {
        if (mPositiveBtn != null) {
            mPositiveBtn.setVisibility(View.VISIBLE);
            mPositiveBtn.setText(btnText);
            mPositiveBtn.setOnClickListener(v -> {
                hide();
                if (listener != null) {
                    listener.onClick(mPositiveBtn);
                }
            });
        }
        return this;
    }

    public MyPopup setNegativeButton(String btnText, View.OnClickListener listener) {
        if (mNegativeBtn != null) {
            mNegativeBtn.setVisibility(View.VISIBLE);
            mNegativeBtn.setText(btnText);
            mNegativeBtn.setOnClickListener(v -> {
                hide();
                if (listener != null) {
                    listener.onClick(mNegativeBtn);
                }
            });
        }
        return this;
    }

    public MyPopup setOnShowListener(DialogInterface.OnShowListener showListener) {
        mShowListener = showListener;
        return this;
    }

    private void onShow() {
        if (mShowListener != null) {
            mShowListener.onShow(null);
        }
    }

    public MyPopup setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
        mDismissListener = dismissListener;
        return this;
    }

    private void onDismiss() {
        if (mDismissListener != null) {
            mDismissListener.onDismiss(null);
        }
    }

    public MyPopup setOnKeyListener(View.OnKeyListener keyListener) {
        if (mParentLayout != null) {
            mParentLayout.setOnKeyListener(keyListener);
        }
        return this;
    }


    private void notifyPopupShow() {
		// todo ...
    }

    private void notifyPopupHide() {
		// todo ...
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377

这样修改,当弹窗起来,底层是没有置灰的效果的,需要修改popup.xml文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#99000000">
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/666367
推荐阅读
相关标签
  

闽ICP备14008679号