作者 liyan

修改状态栏2

  1 +package com.br_technology.securitytrain_master.util
  2 +
  3 +import android.content.Context
  4 +import android.content.res.Resources
  5 +import android.graphics.drawable.ColorDrawable
  6 +import android.view.LayoutInflater
  7 +import android.view.View
  8 +import android.view.ViewGroup
  9 +import android.widget.PopupWindow
  10 +
  11 +
  12 +/**
  13 + * Time: 7/30/2021 11:28
  14 + * Author: Captain
  15 + * Description: 初见时你很迷人
  16 + */
  17 +class PoPWindowUtil {
  18 + private var mPopupWindow: PopupWindow? = null
  19 +
  20 + companion object {
  21 + private var instance:PoPWindowUtil ? = null
  22 + get() {
  23 + if (field == null){
  24 + field = PoPWindowUtil()
  25 + }
  26 + return field
  27 + }
  28 + fun get(): PoPWindowUtil{
  29 + return instance!!
  30 + }
  31 + }
  32 +
  33 + /**
  34 + * popwindow
  35 + *
  36 + * @param context
  37 + * @param anchorView 锚点布局
  38 + * @param layout 弹窗的布局文件
  39 + *
  40 + */
  41 + fun initPopWindow(context: Context?, anchorView: View?, layout: Int): View? {
  42 + // 初始化popUpWindow
  43 + // 生成 View 对象
  44 + val popRootView: View = LayoutInflater.from(context).inflate(layout, null)
  45 + // PopUpWindow 传入 ContentView
  46 + if (mPopupWindow == null) {
  47 + mPopupWindow = PopupWindow(
  48 + popRootView,
  49 + ViewGroup.LayoutParams.MATCH_PARENT,
  50 + ViewGroup.LayoutParams.WRAP_CONTENT,
  51 + true
  52 + )
  53 + }
  54 + // mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);//设置动画
  55 + // 设置背景
  56 + //实例化一个ColorDrawable颜色为半透明,以达到变暗的效果
  57 + val dw = ColorDrawable(-0x50000000)
  58 + mPopupWindow!!.setBackgroundDrawable(dw)
  59 + // 外部点击事件
  60 + mPopupWindow!!.setOutsideTouchable(false)
  61 + // 传入点
  62 + mPopupWindow!!.showAsDropDown(anchorView)
  63 + //关闭时置空对象
  64 + mPopupWindow!!.setOnDismissListener(object : PopupWindow.OnDismissListener {
  65 + override fun onDismiss() {
  66 + mPopupWindow = null
  67 + }
  68 + })
  69 + return popRootView //返回View
  70 + }
  71 +
  72 + /**
  73 + * 获取状态栏高度
  74 + *
  75 + * @param context
  76 + * @return
  77 + */
  78 + fun getStatusBarHeight(context: Context): Int {
  79 + val resources: Resources = context.getResources()
  80 + val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
  81 + return resources.getDimensionPixelSize(resourceId)
  82 + }
  83 +
  84 + /**
  85 + * 关闭方法
  86 + */
  87 + fun dismissPoPWindow() {
  88 + if (mPopupWindow != null) {
  89 + mPopupWindow!!.dismiss()
  90 + }
  91 + }
  92 +
  93 +}