如何在TabLayout更改所选标签的标题颜色?

时间:2017-10-04 08:09:45

标签: android android-styles android-tablayout

我想在TabLayout的图标后面更改此区域的颜色,只有一行带有图标。我还需要在标题下更改此粉红色系列,但我找不到合适的样式参数。我的风格:

  <style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">?attr/colorAccent</item>
    <item name="tabIndicatorHeight">4dp</item>
    <item name="tabPaddingStart">6dp</item>
    <item name="tabPaddingEnd">6dp</item>
    <item name="tabBackground">?attr/selectableItemBackground</item>
    <item name="tabTextAppearance">@style/AppTabTextAppearance</item>
    <item name="tabSelectedTextColor">@color/darkPurple</item>
  </style>

  <!-- for text -->
  <style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/orange</item>
    <item name="textAllCaps">false</item>
  </style>

screenshot

4 个答案:

答案 0 :(得分:2)

尝试使用tab-layout

的属性
app:tabIndicatorColor="@color/colorBlack"// it set Selected tab underline color
app:tabSelectedTextColor="@color/colorPrimary"// it set Selected tab  text color
app:tabTextColor="@color/colorAccent"// it set tab text color

答案 1 :(得分:1)

对于标签背景颜色,请创建selector并将该选择器的资源ID设置为:

<item name="tabBackground">@drawable/selector_name</item>

对于粉红色下划线,请更改

中的颜色

<item name="tabIndicatorColor">@color/your_new_color</item>

答案 2 :(得分:1)

在xml:

中添加属性
<android.support.design.widget.TabLayout
    ....
    app:tabBackground="@drawable/tab_color_selector"
    ...
    />

在drawable文件夹中创建tab_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
    <item android:drawable="@color/tab_background_unselected"/>
</selector>

答案 3 :(得分:0)

试试这个

  • bg_color
  • 中创建drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/timecard"
    android:color="@color/colorText" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/timecard_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/timecard"
    android:color="@color/colorText"/>
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/timecard_selected"
    android:color="@color/colorSkyBlue"/>

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/timecard"
    android:color="@color/colorText"/>
<item android:state_pressed="true" android:drawable="@drawable/timecard_selected"
    android:color="@color/colorSkyBlue" />

  • 并在layout中添加此内容
 <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:tabStripEnabled="false"
            app:tabIndicatorHeight="0dp"
            android:fitsSystemWindows="true"
            app:tabContentStart="0dp"
            app:tabBackground="@drawable/bg_color"
            app:tabPaddingStart="0dp"
            android:paddingLeft="0dp"
            app:tabPaddingEnd="0dp"
            app:paddingEnd="0dp"
            android:layout_gravity="center_vertical"
            app:tabIndicatorColor="@color/colorSkyBlue"
            android:paddingRight="0dp"
            app:tabSelectedTextColor="@color/colorSkyBlue" />