android.support.design.widget.tablayout选项卡指示器颜色

时间:2015-08-19 09:23:37

标签: android android-layout android-tablayout

我正在使用TabLayout和View Pager来显示多个标签。我想更改选定的标签指示器。我使用过app:tabIndicatorColor,但颜色没有变化。它呈现出绿色。我已经读过,默认情况下tabIndicator颜色设置为color/accent,但绿色不是我的强调颜色。 我的xml如下:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@color/primary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            />
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/white"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@color/primary_light" 
/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/header_layout"
    app:menu="@menu/menu_drawer"
    />

我错过了什么? 图片在这里:http://imgur.com/uNcbviv

4 个答案:

答案 0 :(得分:6)

 <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:background="@color/material_blue_grey_800"
    app:tabIndicatorColor="@color/orange"
    app:tabSelectedTextColor="@color/orange"
    app:tabTextColor="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>

它可以帮到你。

 app:tabIndicatorColor="@color/orange"  here is the line which change the color.

答案 1 :(得分:3)

您可以针对TabLayout

尝试以下自定义
<android.support.design.widget.TabLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@id/pages_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@android:color/white"
    app:tabIndicatorHeight="4dp"/>

这应该解决指标颜色!

答案 2 :(得分:0)

根据TabLayout的源代码,tabTextColor必须引用选择器颜色而不是单一颜色。这是源代码中使用getColorStateList

的部分
if (a.hasValue(R.styleable.TabLayout_tabTextColor)) {
    // If we have an explicit text color set, use it instead
    mTabTextColors = a.getColorStateList(R.styleable.TabLayout_tabTextColor);
}

因此您应该在文件中定义颜色,例如res/color/your_colors.xml

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

答案 3 :(得分:0)

您可以像

那样以编程方式设置颜色
tabLayout.setSelectedTabIndicatorColor(R.color.black);