gpt4 book ai didi

android - 如何将设计支持库与 leanback 一起用于 android TV?

转载 作者:行者123 更新时间:2023-11-30 00:28:02 25 4
gpt4 key购买 nike

我有针对 Android TV 应用程序的定制设计,并非所有设计都与 Leanback 库中的小部件匹配。如何使用设计支持库(例如 TabLayout)?它提示我需要使用 AppCompat 主题。我知道谷歌不建议在电视上使用标签。

最佳答案

要在 Leanback 应用程序中创建 TabLayout,您可以使用 ContextThemeWrapper(我用 Kotlin 编写了它):

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val c = ContextThemeWrapper(activity, R.style.Theme_AppCompat)
return LayoutInflater.from(c).inflate(R.layout.my_tablayout, container, false)
}

为了让 DPAD 正常工作,我将 TabLayout 子类化如下:

class FocusableTabLayout(context: Context, attributeSet: AttributeSet): TabLayout(context, attributeSet) {
override fun focusSearch(focused: View?, direction: Int): View? {
val view = super.focusSearch(focused, direction)
if (hasFocus() && view != null) {
if (direction == View.FOCUS_LEFT) {
if (selectedTabPosition > 0) {
getTabAt(selectedTabPosition - 1)?.select()
}
} else if (direction == View.FOCUS_RIGHT) {
if (selectedTabPosition < tabCount) {
getTabAt(selectedTabPosition + 1)?.select()
}
}
}
return view
}

override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean {
val tabStrip = getChildAt(0) as LinearLayout
tabStrip.getChildAt(selectedTabPosition).requestFocus()
return true
}
}

如果您计划将 TabLayoutViewPager 一起使用,请使用此子类来防止一页上的 DPAD 交互触发幻灯片:

class FocusableViewPager(context: Context, attributeSet: AttributeSet): ViewPager(context, attributeSet) {
override fun executeKeyEvent(event: KeyEvent): Boolean {
return false
}
}

关于android - 如何将设计支持库与 leanback 一起用于 android TV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45008602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com