0

我在ViewPager中使用PagerTitleStrip如何爲PagerTitleStrip創建兩種不同的文本顏色?

<android.support.v4.view.ViewPager 
    android:id="@id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <android.support.v4.view.PagerTitleStrip 
     android:id="@id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@style/PagerTitleStrip" 
     /> 
</android.support.v4.view.ViewPager> 

我必須在此條上顯示n個標題。

final int titles[] = {"Title1" , "Title2", "Title3", "Title4"}; 

這是在PagerTitleStrip上應用的樣式。

<style name="PagerTitleStrip"> 
    <item name="android:textColor">@color/red</item> 
    <item name="android:background">@android:color/white</item> 
    <item name="android:textSize">20sp</item> 
</style> 

請注意,此ViewGroup的AttributeSet中只有一個textColor字段。

我想設置兩種不同的顏色(紅色和灰色),一種用於可見頁面的標題(主要標題),另一種用於不可見的頁面(非主要標題在邊緣部分可見)。

此外,在換頁過程中,條形標題應該在轉換過程中改變顏色。

根據文檔,setNonPrimaryAlpha(float)可以讓你定義非主要標題的alpha值,而不是這個,我想要不同的文字顏色,任何人都可以幫助我嗎?

+0

我不確定,但您可以嘗試通過選擇器設置文本顏色。你可以嘗試說明哪些​​對你有用。祝你好運。 – 2014-09-26 07:14:30

+0

這根本不可能。如果你想要做這樣的事情,你必須編寫你自己的PagerTitleStrip實現。 – reVerse 2014-09-26 08:41:05

+0

@hbansal你有沒有找到解決方案?請告訴我。 – AB1209 2015-08-06 10:46:00

回答

2

您可以通過XML做到這一點,你必須做的唯一的事情就是寫你的顏色狀態列表:

pager_tab_strip_text_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:color="@color/background_black_3" android:state_window_focused="false" /> 
    <item android:color="@color/highlight_gold" android:state_window_focused="true" /> 

</selector> 

把一個樣式裏面的顏色狀態列表:

<style name="PagerTabStrip"> 
     <item name="android:textColor">@color/pager_tab_strip_text_selector</item> 
    </style> 

您必須做的唯一事情就是,不要將其設置爲樣式,而應將其設置爲文本外觀,如下所示:

<android.support.v4.view.ViewPager 
     android:id="@+id/fragment_light_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v4.view.PagerTitleStrip 
      android:id="@+id/fragment_light_pagertabstrip" 
      android:textAppearance="@style/PagerTabStrip.Light" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" /> 

    </android.support.v4.view.ViewPager> 
相關問題