Android笔记(二十七):自定义Dialog实现居中Toast

背景

记录实现符合项目需求的Toast弹窗

具体实现

class MyTipDialog private constructor(val context: Activity): Dialog(context, R.style.MyTipTheme) {

    val resId = ObservableField(0)
    private val mainHandler = Handler(Looper.getMainLooper())
    init {
        setCanceledOnTouchOutside(true)
        setCancelable(true)
        window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        window?.setBackgroundDrawableResource(Color.parseColor("#00000000"))
        window?.setWindowAnimations(R.style.myTipDialogAnim)
        val binding = DataBindingUtil.inflate<MyTipsBinding>(layoutInflater, R.layout.my_tips, null, false)
        binding.viewModel = this
        setContentView(binding.root)
    }

    companion object{
        private var instance: MyTipDialog ? = null
        fun getInstance(context: Activity, resId: Int): MyTipDialog {
            if (instance == null){
                instance = MyTipDialog (context)
            }
            instance?.dismiss()
            instance?.resId?.set(resId)
            return instance!!
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        context.dispatchTouchEvent(ev)
        return super.dispatchTouchEvent(ev)
    }

    override fun show() {
        super.show()
        mainHandler.postDelayed({
            dismiss()
        }, 2000)
    }

    override fun dismiss() {
        super.dismiss()
        mainHandler.removeCallbacksAndMessages(null)
    }
}

自定义布局:R.layout.my_tips


<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="com.masked_boy.test.MyTipsDialog" />
    data>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="304dp"
            android:paddingHorizontal="20dp"
            android:paddingVertical="5dp"
            android:layout_gravity="center"
            android:text="@{context.getString(viewModel.resId)}"
            android:textSize="11sp"
            android:textColor="#FFFFFFFF"
            android:background="#000000"/>
    FrameLayout>
layout>

对话框style


<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="MyTipTheme" parent="AppTheme">
        "android:windowFullscreen">false
        "android:windowLightStatusBar" tools:targetApi="m">true
    style>
    <style name="myTipDialogAnim" mce_bogus="1" parent="android:Animation">
        "android:windowEnterAnimation">@anim/my_tip_dialog_enter_anim
        "android:windowExitAnimation">@anim/my_tip_dialog_exit_anim
    style>
resources>

对话框show动画R.anim.my_tip_dialog_enter_anim


<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />
set>

对话框dismiss动画R.anim.my_tip_dialog_exit_anim


<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0" />
set>

你可能感兴趣的:(Android笔记,android,笔记)