2017-07-10 123 views
1

我有Android电视应用程序的自定义设计,不是所有人都匹配leanback库中的小部件。 如何使用设计支持库(例如TabLayout)?它抱怨我需要使用AppCompat主题。 我知道谷歌不建议使用电视上的标签。如何将设计支持库与leanback一起用于android TV?

+0

您是否找到任何解决方案? – tmm1

+0

我写了自己的TabLayout,没有找到一个将AppCompat与leanback一起使用的解决方案。 –

回答

1

要创建TabLayout在Leanback的应用程序,你可以使用ContextThemeWrapper

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 
    } 
} 
相关问题