android – 如何在PagerTabStrip中更改TabIndicater的颜色

我有一个在xml中定义的 android.support.v4.view.PagerTabStrip,如下所示:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--
    This title strip will display the currently visible page title,as well as the page
    titles for adjacent pages.
    -->
    <android.support.v4.view.PagerTabStrip android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

</android.support.v4.view.ViewPager>

如何更改TabIndicater颜色?它可以在setTabIndicatorColor(int color)之前以编程方式进行更改,但我需要在xml中找到一种方法.

解决方法

主要方法是使类扩展为PagerTabStrip,例如名为MyPagerTabStrip的类:
package com.example.View; /// change this package to yourselves.

public class MyPagerTabStrip extends PagerTabStrip
{
    public MyPagerTabStrip(Context context,AttributeSet attrs)
    {
    super(context,attrs);
    final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyPagerTabStrip);
    setTabIndicatorColor(a.getColor(
            R.styleable.MyPagerTabStrip_indicatorColor,Color.BLUE));
    a.recycle();
    }

}

在res / values文件夹中新建一个名为attrs.xml的文件,该文件内容为:

<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="MyPagerTabStrip">
    <attr name="indicatorColor" format="reference|color" />
</declare-styleable>

相同的文件夹,如果不存在则新建一个文件,名为styles.xml,将下面的代码放在文件中:

<style name="Pager">
    <item name="android:textColor">#ffffff</item>
    <item name="indicatorColor">#6f8dc5</item>
</style>

最后,您的布局xml文件是:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!--
This title strip will display the currently visible page title,as well as the page
titles for adjacent pages.
-->
<com.example.view.MyPagerTabStrip android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:textColor="#fff"
    android:paddingTop="4dp"
    android:paddingBottom="4dp" />

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...