簡體   English   中英

android.support.design.widget.tablayout選項卡指示器顏色

[英]android.support.design.widget.tablayout Tab Indicator colour

我正在使用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

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

您可以為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"/>

這應該解決指標顏色!

根據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>

你可以像編程一樣設置顏色

tabLayout.setSelectedTabIndicatorColor(R.color.black);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM