如何解决使用D-Pad的窗口管理器的焦点问题
我正在使用窗口管理器在智能电视应用中进行通知,但在窗口上显示视图时遇到了一些问题,因此我无法处理焦点,也无法使用D-Pad获取子视图的单击事件。 / p>
我已经在Google上找到了,但没有找到解决此问题的任何方法。
使用窗口管理器显示通知视图:-
我们可以从Activity中调用此类:-
val notificationView = NotificationView(this)
notificationView.addView()
科特琳代码:-NotificationView.kt
class NotificationView(val context: Context) {
var notificationWM: WindowManager? = null
var layoutParams: WindowManager.LayoutParams? = null
fun addView(): View {
val view: ViewGroup = LayoutInflater.from(context).inflate(
R.layout.layout_overlay,null
) as ViewGroup
if (notificationWM != null) {
notificationWM!!.updateViewLayout(view,layoutParams)
} else {
notificationWM = getWindowManager()
notificationWM!!.addView(view,layoutParams)
}
initView(view)
return view
}
private fun getWindowManager(): WindowManager {
val LAYOUT_FLAG: Int
LAYOUT_FLAG = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,LAYOUT_FLAG,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT
)
params.gravity = Gravity.CENTER_VERTICAL
params.gravity = Gravity.RIGHT or Gravity.TOP
params.title = "Load Average"
layoutParams = params
return context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
}
private fun initView(view: View) {
val actionButton = view.findViewById<Button>(R.id.btn_action)
val message = view.findViewById<TextView>(R.id.tv_message)
actionButton.setOnClickListener {
Toast.makeText(context,"Button Click Now",Toast.LENGTH_LONG).show()
}
actionButton.requestFocus()
}
}
布局代码(此布局将在使用窗口管理器的通知中显示):-layout_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="150dp"
android:background="@drawable/selector_notification_btn"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/tv_message"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:fontFamily="@font/montserrat_regular"
android:text="@string/my_profile"
android:textSize="25dp" />
<Button
android:id="@+id/btn_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:focusableInTouchMode="true"
android:focusable="true"
android:background="@drawable/selector_notification_btn"
android:fontFamily="@font/montserrat_regular" />
</LinearLayout>
通知视图显示完美,但是我没有使用D-Pad获得按钮的单击事件
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。