|
|
package com.br_technology.securitytrain_master.util
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.content.res.Resources
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
import android.widget.PopupWindow
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Time: 7/30/2021 11:28
|
|
|
* Author: Captain
|
|
|
* Description: 初见时你很迷人
|
|
|
*/
|
|
|
class PoPWindowUtil {
|
|
|
private var mPopupWindow: PopupWindow? = null
|
|
|
|
|
|
companion object {
|
|
|
private var instance:PoPWindowUtil ? = null
|
|
|
get() {
|
|
|
if (field == null){
|
|
|
field = PoPWindowUtil()
|
|
|
}
|
|
|
return field
|
|
|
}
|
|
|
fun get(): PoPWindowUtil{
|
|
|
return instance!!
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* popwindow
|
|
|
*
|
|
|
* @param context
|
|
|
* @param anchorView 锚点布局
|
|
|
* @param layout 弹窗的布局文件
|
|
|
*
|
|
|
*/
|
|
|
fun initPopWindow(context: Context?, anchorView: View?, layout: Int): View? {
|
|
|
// 初始化popUpWindow
|
|
|
// 生成 View 对象
|
|
|
val popRootView: View = LayoutInflater.from(context).inflate(layout, null)
|
|
|
// PopUpWindow 传入 ContentView
|
|
|
if (mPopupWindow == null) {
|
|
|
mPopupWindow = PopupWindow(
|
|
|
popRootView,
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
true
|
|
|
)
|
|
|
}
|
|
|
// mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);//设置动画
|
|
|
// 设置背景
|
|
|
//实例化一个ColorDrawable颜色为半透明,以达到变暗的效果
|
|
|
val dw = ColorDrawable(-0x50000000)
|
|
|
mPopupWindow!!.setBackgroundDrawable(dw)
|
|
|
// 外部点击事件
|
|
|
mPopupWindow!!.setOutsideTouchable(false)
|
|
|
// 传入点
|
|
|
mPopupWindow!!.showAsDropDown(anchorView)
|
|
|
//关闭时置空对象
|
|
|
mPopupWindow!!.setOnDismissListener(object : PopupWindow.OnDismissListener {
|
|
|
override fun onDismiss() {
|
|
|
mPopupWindow = null
|
|
|
}
|
|
|
})
|
|
|
return popRootView //返回View
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取状态栏高度
|
|
|
*
|
|
|
* @param context
|
|
|
* @return
|
|
|
*/
|
|
|
fun getStatusBarHeight(context: Context): Int {
|
|
|
val resources: Resources = context.getResources()
|
|
|
val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
|
|
|
return resources.getDimensionPixelSize(resourceId)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 关闭方法
|
|
|
*/
|
|
|
fun dismissPoPWindow() {
|
|
|
if (mPopupWindow != null) {
|
|
|
mPopupWindow!!.dismiss()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|