【问题标题】:TabLayout color of unselected tab underline未选中选项卡下划线的 TabLayout 颜色
【发布时间】:2019-05-20 20:49:31
【问题描述】:

在这张图中,在tablayout中,选中的tabbar下划线颜色为紫色,文字。

我搜索了未选中的标签栏,但找不到未选中的标签栏下划线。

我想在选择某个标签时更改颜色,更改未选择的标签栏下划线颜色。

如果你知道这件事,你会帮助我吗?

【问题讨论】:

    标签: android android-viewpager android-tablayout


    【解决方案1】:

    在你的drawable文件夹中创建一个xml文件

    custom_indicator.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>
    

    并在你的tabLayout中设置这个drawable

    <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:tabBackground="@drawable/custom_indicator" />
    

    要更改未选择的选项卡文本颜色,只需提供默认选项卡文本颜色和选定选项卡文本颜色,如下所示:

    <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/custom_indicator" />
    

    【讨论】:

    • 谢谢,但在这种情况下,未选择的下划线颜色会发生变化,但选择的颜色不会改变。
    • 我想我在 custom_indicator 中又制作了一项并将其更改为 state_selected='true' 但它不会发生。您的编辑是正确的。谢谢
    • 谢谢!如果对您有用,您可以将其标记为已解决
    • 如果我想动态更改未选择的颜色怎么办?如果不使用选择器通过可绘制对象,就找不到方法
    【解决方案2】:

    您只需使用 android:background 为所有未选中的标签设置颜色一次。

        <style name="tab_text_style">
            <item name="android:textSize">16sp</item>
            <item name="android:fontFamily">sans-serif</item>
            <item name="android:textStyle">bold</item>
        </style>
    
        <style name="tab_style">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="tabIndicatorColor">@android:color/white</item>
            <item name="tabIndicatorHeight">3dp</item>
            <item name="tabTextAppearance">@style/tab_text_style</item>
            <item name="tabSelectedTextColor">@android:color/white</item>
            <item name="tabTextColor">@color/inactive_gray</item>
            <item name="android:background">@drawable/custom_inactive_tab_indicator</item>
            <item name="tabGravity">fill</item>
            <item name="tabMode">fixed</item>
        </style>
    

    custom_inactive_tab_indicator.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:left="-4dp"
            android:right="-4dp"
            android:top="-4dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="3dp"
                    android:color="#57595f" />
            </shape>
        </item>
    </layer-list>
    
    

    activity.xml

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                style="@style/tab_style" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2016-03-01
      • 2013-04-14
      • 2016-04-06
      • 1970-01-01
      相关资源
      最近更新 更多