Android TabLayout 选中标签的背景

3

是否真的没有一种简单的方法来使用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>
1个回答

3
样式变更
  <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> 

我觉得你误解了我的意思,我想让指示器可用并显示,而不是隐藏! - Zuop
1
<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> - loadjang
是否有可能拥有像上面那样的自定义背景并保持涟漪效果?(触摸时的涟漪) - Steve McMeen
这段代码在自定义选项卡视图上无法正常工作,而在默认的选项卡布局上却可以正常工作。 - Erum
这种方法的缺点是它适用于应用程序中的所有TabLayout。在某些情况下,您可能真的不希望这样,因此使用<selector>并将其设置为tabLayout背景的方法可能更可取。 - Marino
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接