繁体   English   中英

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

[英]How to change a color of the title of the selected tab at 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>

屏幕截图

尝试使用以下选项卡布局的属性

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

对于选项卡的背景色,创建一个选择器 ,然后将该选择器的资源ID放入:

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

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

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

在xml中添加属性:

<android.support.design.widget.TabLayout
    ....
    app:tabBackground="@drawable/tab_color_selector"
    ...
    />

并在可绘制文件夹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>

尝试这个

  • drawable folder创建bg_color

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM