问题描述
我为辅助监视器创建了Presentation Class。我想在第二个监视器上显示一些对话框。
对于吐司,我发现了这一点:Display Toast message in Presentation Display with presentation context
但这没用。
我唯一能想到的就是在我的基本布局上建立新的视图。 但是,它无法像Dialog一样容易地控制视图。
这是我的炸玉米饼。我将其更改为使用Tostada.makeText(context,text,time)
。
object Tostada {
private var mView: TextView? = null
private var mWM: WindowManager? = null
private val mParams = WindowManager.LayoutParams()
private var mContext: Context? = null
private val mHideRunnable = Runnable { handleHide() }
/**
* Assign the Tostada context. It can be a Presentation context. Remember to call setContext(null)
* to clear the stored context when you no longer need this class,otherwise memory leaks can occur.
*
* @param context The context where the Tostada will be rendered
*/
/**
* Creates a Tostada with a gravity ana an offset x,y
*
* @param offsetX The X offset in pixels to apply to the gravity's location.
* @param offsetY The Y offset in pixels to apply to the gravity's location.
* @paran gravity The gravity constant
*/
fun makeText(context: Context,text: String?,timeout: Int) {
mContext = context
mParams.packageName = context.packageName
mView = TextView(context)
mWM = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val params = mParams
params.height = WindowManager.LayoutParams.WRAP_CONTENT
params.width = WindowManager.LayoutParams.WRAP_CONTENT
params.format = PixelFormat.TRANSLUCENT
params.windowAnimations = 0
params.type = WindowManager.LayoutParams.TYPE_TOAST
params.title = "Toast"
params.flags = (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
mParams.gravity = Gravity.BottOM or Gravity.CENTER_VERTICAL
mParams.x = 0
mParams.y = 50
mParams.verticalMargin = 0f
mParams.horizontalMargin = 0f
checkNotNull(mContext) { "You must assign a context to this Tostada before it vcan be used" }
// remove old timer for an ongoing tostada,if any
mView!!.removeCallbacks(mHideRunnable)
mView!!.text = text
// Add the view to the window manager,if not already added
if (mView!!.parent == null) mWM!!.addView(mView,mParams)
// schedule for removal
if (timeout > 0) mView!!.postDelayed(mHideRunnable,timeout.toLong())
}
private fun handleHide() {
// remove the view if added
if (mView!!.parent != null) mWM!!.removeView(mView)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)