How to use popup elegantly in Android, pop up at the click position, personal records

The first step is to define the initialization popup method, set some parameters, and then bind the ui layout


    lateinit var duishoujiaTypePop: PopupWindow
    lateinit var duishoujiaTypeInflate: View
    lateinit var tv_duishoujia_type_duishoujia: TextView
    lateinit var tv_duishoujia_type_zuiyouwudang: TextView
    lateinit var tv_duishoujia_type_zuiyoushidang: TextView
    lateinit var tv_duishoujia_type_zuiyouershidang: TextView

    private fun initDuishoujiaType() {
        //填充对话框的布局
        duishoujiaTypeInflate = layoutInflater.inflate(
                R.layout.dialog_yongxu_duishoujia_type, LinearLayout(context),
                false
        )
//        val height = getUnknownDpSize(context!!, 300)// 屏幕的高
//        val width = ll_weituo_type.measuredWidth
        duishoujiaTypePop = PopupWindow(
                duishoujiaTypeInflate,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        )
        //设置
        duishoujiaTypePop!!.setClippingEnabled(false)
        // 设置动画效果
        duishoujiaTypePop!!.animationStyle = R.style.ActionSheetDialogAnimation
        //设置点击外部
        duishoujiaTypePop!!.setOutsideTouchable(true)
        //点击外部控件时不执行外部事件,拦截返回键
        duishoujiaTypePop!!.isFocusable = true
        duishoujiaTypePop!!.setBackgroundDrawable(BitmapDrawable())
        //设置其他位置取消阴影
        duishoujiaTypePop!!.setOnDismissListener {
            backgroundAlpha(1.0F)
        }
        tv_duishoujia_type_duishoujia = duishoujiaTypeInflate!!.findViewById(R.id.tv_duishoujia_type_duishoujia)
        tv_duishoujia_type_duishoujia.setOnClickListener {
            duishoujiaType = "1"
            tv_duishoujia_value.text = getString(R.string.competitors_price)
            duishoujiaTypePop.dismiss()
        }
        tv_duishoujia_type_zuiyouwudang = duishoujiaTypeInflate!!.findViewById(R.id.tv_duishoujia_type_zuiyouwudang)
        tv_duishoujia_type_zuiyouwudang.setOnClickListener {
            duishoujiaType = "5"
            tv_duishoujia_value.text = getString(R.string.optimal_five_gears)
            duishoujiaTypePop.dismiss()
        }
        tv_duishoujia_type_zuiyoushidang = duishoujiaTypeInflate!!.findViewById(R.id.tv_duishoujia_type_zuiyoushidang)
        tv_duishoujia_type_zuiyoushidang.setOnClickListener {
            duishoujiaType = "10"
            tv_duishoujia_value.text = getString(R.string.optimal_ten_gears)
            duishoujiaTypePop.dismiss()
        }
        tv_duishoujia_type_zuiyouershidang = duishoujiaTypeInflate!!.findViewById(R.id.tv_duishoujia_type_zuiyouershidang)
        tv_duishoujia_type_zuiyouershidang.setOnClickListener {
            duishoujiaType = "20"
            tv_duishoujia_value.text = getString(R.string.optimal_two_gears)
            duishoujiaTypePop.dismiss()
        }
    }

The second step is to call the initialization method declared in the first step before opening the pop-up window, which can be called in onCreate, and then add components to click on the event to open the pop-up window. The code explanation has been written in the comment, and you can understand it by yourself. , the layout inside is ok if you delete and rewrite it yourself, you don’t have to copy my layout, so I don’t put the layout here either.


        initDuishoujiaType()

        ll_duishoujia_value.setOnClickListener {
            //这里传入的布局是点击的组件,也就是在什么位置显示
            duishoujiaTypePop!!.showAsDropDown(
                    view!!.findViewById(R.id.ll_duishoujia_value)
            )
            //设置弹窗背景阴影
            backgroundAlpha(0.7f)
        }

Guess you like

Origin blog.csdn.net/Spy003/article/details/128945224