选项卡布局 | 选项卡指示器未选中颜色

3

我正在使用 TabLayout

当选项卡没有被选择时,我需要设置TabIndicator的颜色。

这就是我想做的 -

enter image description here

这是我已经完成的 -

enter image description here

2个回答

2

使用app:tabTextColor来实现此功能。

  • 未选中时,它将显示app:tabTextColor

  • 选中时,它将显示app:tabSelectedTextColor

请使用以下代码:

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:background="@color/colorPrimary"
    app:tabIndicatorColor="@color/white"
    app:tabIndicatorHeight="4dp"
    app:tabSelectedTextColor="@color/white"
    app:tabTextColor="#AAAAAA"
    app:tabTextAppearance="@style/NavigationTabTextAppeareance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    >

styles.xml : 用于加粗标签文本

 <style name="NavigationTabTextAppeareance" parent="TextAppearance.Design.Tab">
    <item name="android:textStyle">bold</item>
</style>

对于未选择的标签底部颜色:

参考:TabLayout color of unselected tab underline


当行未被选中时,我需要更改其颜色。 - Shoeb Siddique
请查看@ShoebSiddique的这个链接:https://dev59.com/V6Tia4cB1Zd3GeqP9iyK - Abhishek kumar
好的,让我来检查一下... - Shoeb Siddique

0

您可以使用 tabtextColor 来设置默认颜色,当选项卡未被选中时。

在 drawable 中创建名为 underlinedrawable.xml 的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Bottom indicator color for the UNSELECTED tab state -->
        <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
            <shape android:shape="rectangle">
                <stroke android:color="#65acee" android:width="2dp"/>
            </shape>
        </item>
    </layer-list>
</item>
</selector>

要更改选项卡文本颜色下划线,请在主xml中添加underlinedrawable

<android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabTextColor="@color/unselected_color"
            app:tabSelectedTextColor="@color/selected_color"
            app:tabBackground="@drawable/underlinedrawable" />

当行未被选中时,我需要更改其颜色。 - Shoeb Siddique
@ShoebSiddique 请查看编辑答案。 - Bunny
1
可能是重复的 https://dev59.com/V6Tia4cB1Zd3GeqP9iyK#45582178 - Vidhi Dave
我尝试了这段代码,但它没有起作用。 - Shoeb Siddique
@Bunny 给出参考,不要复制粘贴。 - Vidhi Dave
显示剩余2条评论

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