6

Ho un android.support.v4.view.PagerTabStrip definiti in XML come segue:Come modificare il colore di TabIndicater in 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> 

Come posso cambiare il colore TabIndicater? Può essere modificato programmaticamente da setTabIndicatorColor(int color) ma ho bisogno di trovare un modo per farlo in xml.

risposta

9

Il metodo principale è quello di fare una classe estende PagerTabStrip, per esempio, la classe denominata 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(); 
    } 

} 

nella cartella res/valori nuovo un file di nome attrs.xml, il contenuto di questo file è:

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

<declare-styleable name="MyPagerTabStrip"> 
    <attr name="indicatorColor" format="reference|color" /> 
</declare-styleable> 

stessa cartella, un nuovo file, se non esiste, il nome styles.xml, ha messo il codice qui sotto nel file:

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

Infine, il file xml layout è:

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

+0

Mi sono perso qualcosa? Per cosa è lo stile/Cercapersone? Non l'hai usato affatto. – nevermind

Problemi correlati