【问题标题】:White tab with dark text带有深色文本的白色标签
【发布时间】:2023-03-08 18:13:01
【问题描述】:

我正在使用TabLayout 并尝试将tabbackground 设置为带有深色文本的白色

我似乎无法更改文本的颜色,而且它似乎保持白色。

这是我使用的样式:

<style name="TabWhiteText" parent="Base.TextAppearance.AppCompat">
    <item name="android:textColor">@color/deepEggplant</item>
</style>

<style name="TabWhite" parent="Widget.Design.TabLayout">
    <item name="tabSelectedTextColor">@color/deepEggplant</item>
    <item name="tabBackground">@android:color/white</item>
    <item name="tabIndicatorColor">@android:color/white</item>
</style>

<style name="TabWhiteMedium" parent="TabWhite">
    <item name="android:textAppearance">@style/TabWhiteText</item>
</style>

并应用于此:

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:layout_alignParentTop="true"
        style="@style/TabWhiteMedium"
        selectFromListView:tabMode="scrollable" />

【问题讨论】:

    标签: android android-styles android-tablayout


    【解决方案1】:

    这里有一个很好的教程:http://guides.codepath.com/android/google-play-style-tabs-using-tablayout

    您可以将textColor 与深色或标签的背景一起使用为白色:

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabMaxWidth">@dimen/tab_max_width</item>
        <item name="tabIndicatorColor">?attr/colorAccent</item>
        <item name="tabIndicatorHeight">2dp</item>
        <item name="tabPaddingStart">12dp</item>
        <item name="tabPaddingEnd">12dp</item>
        <item name="tabBackground">@android:color/white</item>
        <!-- Your white color maybe -->
        <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
        <item name="tabSelectedTextColor">?android:textColorPrimary</item>
    </style>
    
    <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">#000000</item>
        <!-- Or your TabLayout textcolor to dark maybe -->
        <item name="textAllCaps">true</item>
    </style>
    

    那么就用它作为TabLayout的样式吧:

    <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            style="@style/MyCustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </android.support.design.widget.TabLayout>
    

    或其他自定义。


    更新:另外,无需添加样式,只需:

    app:tabBackground="@android:color/white"
    app:tabTextColor="@color/darkcolor"
    

    即:

    <android.support.design.widget.TabLayout
              android:id="@+id/tab_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="?attr/actionBarSize"
              app:tabIndicatorColor="#ffffff"
              app:tabIndicatorHeight="4dp"
              app:tabBackground="@android:color/white" 
              app:tabTextColor="@color/darkcolor"  
              app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

    【讨论】:

    • 试过了(换我的颜色),但还是不行。我将尝试链接中的详细信息并返回。
    • @LinX64 请来chat
    【解决方案2】:

    您可以使用setTabTextColors() 方法以编程方式为标签文本添加颜色。请参阅文档 TabLayout

    要添加颜色资源,请使用 ContextCompat.getColor(context,your color resource id here) 而不是直接将资源作为参数提供

    【讨论】:

    • 试过 tabLayout.setTabTextColors(R.color.deepEggplant, R.color.deepEggplant);但是还是不行。 :(
    • 尝试将颜色添加为 ContextCompat.getColor(context,R.color.deepEggPlant)
    • 原来它使用的是自定义视图!只需要在那里更改 TextColor 。请原谅我出去射击自己!
    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多