自定义Dialog

2021-05-20  本文已影响0人  Method

自定义Dialog类

class BackDialog(context: Context) : AlertDialog(context, R.style.MDialog) {
    private var sureListener: (() -> Unit)? = null
    private var cancelListener: (() -> Unit)? = null

    init {
        /*加载自定义布局*/
        val view = LayoutInflater.from(context).inflate(R.layout.layout_back_dialog, null, false)
        val mSure = view.findViewById<Button>(R.id.mSure)
        val mCancel = view.findViewById<Button>(R.id.mCancel)
        setView(view)
        /*设置监听事件*/
        mSure.setOnClickListener {
            if (sureListener == null) {
                dismiss()
                if(context is Activity){
                    context.finish()
                }
            }
            sureListener?.invoke()
        }

        mCancel.setOnClickListener {
            if (cancelListener == null) {
                dismiss()
            }
            cancelListener?.invoke()
        }
    }

    /*设置监听事件*/
    fun setClickListener(sureListener: () -> Unit, cancelListener: () -> Unit) {
        this.sureListener = sureListener
        this.cancelListener = cancelListener
    }
    /*
    *  window属性需要放在show后边
    */
    override fun show() {
        super.show()
        /*设置进出动画*/
        window?.setWindowAnimations(R.style.backDialog)
        /*窗口居中*/
        window?.setGravity(Gravity.CENTER)
        /*添加Flag*/
        window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        /*获取window宽高*/
        val d = window?.windowManager?.defaultDisplay
        val height = d?.height
        val width = d?.width
        /*自定义宽高*/
        val a = window?.attributes
        a?.width = (width!!*0.75).toInt()
        a?.height = (width*0.65).toInt()
        window?.attributes = a
    }

}

样式

<style name="MDialog" >
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--//设置dialog的背景,此处为系统给定的透明值 -->
        <item name="android:windowFrame">@null</item>
        <!--//Dialog的windowFrame框为无 -->
        <item name="android:windowNoTitle">true</item>        
        <!--//是否显示标题-->
        <item name="android:windowIsFloating">true</item>
        <!--//是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">false</item>
        <!--//是否半透明 -->
        <item name="android:windowContentOverlay">@null</item>
        <!--//是否有覆盖 -->
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <!--//设置Activity出现方式 -->
        <item name="android:backgroundDimEnabled">false</item>
        <!--//背景是否模糊显示 -->
    </style>

布局

<?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:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/mTip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/tip_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/mContent"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="@string/finsh_app_text"
        app:layout_constraintBottom_toTopOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mTip" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toTopOf="@+id/mCancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/mSure"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/sure_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/mCancel"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/mCancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/cancel_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/mSure" />
</androidx.constraintlayout.widget.ConstraintLayout>

window动画

 <style name="backDialog" >
        <item name="android:windowEnterAnimation">@anim/dialog_enter</item>
        <item name="android:windowExitAnimation">@anim/dialog_exit</item>
    </style>
    

enter

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200">
    <translate android:fromYDelta="-100%"
        />
</set>

exit

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200">
    <translate android:fromYDelta="0"
        android:toYDelta="100%"/>
</set>
dialog.gif
上一篇下一篇

猜你喜欢

热点阅读