6

Tôi có một android.support.v4.view.PagerTabStrip quy định tại xml như sau:Làm thế nào để thay đổi màu sắc của TabIndicater trong 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> 

Làm thế nào tôi có thể thay đổi màu sắc TabIndicater? Nó có thể được thay đổi programmaticaly bởi setTabIndicatorColor(int color) nhưng tôi cần phải tìm một cách để làm điều này trong xml.

Trả lời

9

Phương pháp chính là làm cho một lớp học kéo dài PagerTabStrip, ví dụ, lớp tên 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(); 
    } 

} 

trong res thư mục/giá trị mới một file có tên attrs.xml, nội dung của tập tin này là:

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

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

cùng thư mục, mới một tập tin nếu không tồn tại, tên styles.xml, đặt mã dưới đây vào file:

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

Cuối cùng, tập tin xml bố trí của bạn là:

<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

Tôi đã bỏ lỡ điều gì đó chưa? Phong cách/Máy nhắn tin cho là gì? Bạn đã không sử dụng nó cả. – nevermind

Các vấn đề liên quan