android kotlin自定义popupwindow

class AlertPopup(private val activity: Activity):PopupWindow(activity) {

    val binding:PopupAlertBinding by lazy {
        PopupAlertBinding.inflate(LayoutInflater.from(activity))
    }

    init {
        contentView=binding.root
        contentView.setOnClickListener { dismiss() }
        initView()
        width= ViewGroup.LayoutParams.MATCH_PARENT
        height= ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable=true
        isFocusable=true
        setBackgroundDrawable(ColorDrawable(0x55000000))
    }

    var onOkListener:(()->Unit)?=null

    private fun initView(){
        binding.llContent.setOnClickListener {  }
        binding.tvCancel.setOnClickListener {
            dismiss()
        }
        binding.tvOk.setOnClickListener {
            onOkListener?.invoke()
        }
    }

    fun show(view: View){
        if(activity.window.decorView.windowToken!=null){
            showAtLocation(view,Gravity.CENTER,0,0)
        }
    }
}

你可能感兴趣的:(android,android,kotlin,ui)