Android TabLayout 选中的tab背景

标签 android android-viewpager android-tabs android-tablayout

真的没有简单的方法来使用 TabLayout 并能够设置选项卡的颜色(选中,未选中)吗?就像选中的标签背景使用 colorPrimary,未选中的标签使用 colorPrimaryDark 之类的?我在网上搜索过 thisthis以及更多。我可以使用解决方案 1 更改背景颜色,但现在指示器丢失了,我想要它回来。

这不是很难做到..

missing indicator

第一个链接的解决方法:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
        <item name="tabBackground">@drawable/tab_background</item>
</style>

// tab_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
    <item android:drawable="@drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>

回答:

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
        <item name="tabBackground">@drawable/tab_background</item>
        <item name="tabIndicatorColor">@color/colorAccent</item>
        <item name="tabIndicatorHeight">3dp</item>
</style>

最佳答案

风格改变

  <style name="Base.Widget.Design.TabLayout" parent="android:Widget">
      <item name="tabBackground">@drawable/tab_background</item>
      <item name="tabIndicatorColor">#000000</item> 
      <item name="tabIndicatorHeight">5dp</item>      
  </style> 

关于Android TabLayout 选中的tab背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202585/

相关文章:

android - 来自图像 URL GoogleMap API V2 的自定义标记图标

android - 返回在内部 fragment 中不起作用

android - 如何在 Android 的 TabLayout 中以编程方式添加选项卡

android - 如何在操作栏中创建选项卡?

android - 如何从 Android App Bundle 中排除 ABI?

android - 数组适配器 : Json data to be sent to spinner

android - 为什么 View 层次结构会影响GestureDetector的行为?

android - 使用 setCurrentItem(int index) 时,android PageAdapter 和 ViewPager 是否检查 indexoutofbounds 异常?

android - ActionBarSherlock + Tabbed Fragments + ViewPager inside a fragment

android - 如何在Tab中显示ListView?