【问题标题】:android.support.design.widget.tablayout Tab Indicator colourandroid.support.design.widget.tablayout 选项卡指示器颜色
【发布时间】:2015-11-12 12:22:26
【问题描述】:

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

【问题讨论】:

  • @EranBoudjnah 我已经尝试过该解决方案。答案就是我上面的代码。
  • 您是否尝试过建议您以编程方式更改它的解决方案之一?
  • 是的。不会改变任何东西。

标签: android android-layout android-tablayout


【解决方案1】:
 <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.

【讨论】:

  • 如果你没有看原贴的代码,我已经试过app:tabIndicatorColor
  • 如果要改变指标的重力怎么办?
【解决方案2】:

您可以尝试为您的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"/>

这应该可以解决指标颜色问题!

【讨论】:

  • 如何在java中设置?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    你可以像编程一样设置颜色

    tabLayout.setSelectedTabIndicatorColor(R.color.black);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多