【发布时间】:2021-06-07 17:28:35
【问题描述】:
我正在尝试为我的 android 电视应用程序创建自定义布局,而不是使用来自 Leanback 组件的片段。我在开发人员文档中读到,为了突出显示选定的视图,我们需要将 focusable 和 focusableInTouchMode 设置为 true。 然而,即使我已经设置了这些属性,这似乎对我不起作用。
我错过了什么?
activity_video.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.leanback.tab.LeanbackViewPager
android:id="@+id/bubbleViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/button_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" android:state_selected="true"/>
<item android:drawable="@android:color/transparent" android:state_selected="false" />
</selector>
【问题讨论】:
-
button_bg 是 StateListDrawable 吗?您不需要设置 focusable 或 focusableInTouchMode 属性,因为默认情况下按钮是可聚焦的。
-
@IanG.Clifton,是的,我知道 focusable 和 focusableInTouchMode 的默认值,即使没有它也不起作用。是的 button_bg 是 stateListDrawable,已经用该代码更新了 qn。
标签: android android-tv leanback