2012-11-15 42 views
5

我需要在ViewPager中爲我的Android應用程序更改PagerTabStrip的PagerTtrip的字體顏色。這是相同的xml佈局。有什麼辦法可以做到這一點?爲我的Android應用程序在ViewPager中的PagerTabStrip自定義字體顏色

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

     <android.support.v4.view.PagerTabStrip 
     android:id="@+id/pgstrip" 
     android:layout_width="fill_parent" 
     android:background="@color/strip" 
     android:layout_height="@dimen/pagerstripht" 
     android:layout_gravity="top" 
     /> 

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

回答

10

如果你還沒有發現呢,代碼如下

PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_title_strip); 
pagerTabStrip.setDrawFullUnderline(true); 
pagerTabStrip.setTabIndicatorColor(Color.RED); 
10
在你的代碼

<android.support.v4.view.PagerTabStrip 
     android:id="@+id/pgstrip" 
     android:layout_width="fill_parent" 
     android:background="@color/strip" 
     android:layout_height="@dimen/pagerstripht" 
     android:layout_gravity="top" 
     /> 

我想添加機器人:文字顏色= 「#000」 將改變文字顏色。

+3

不確定爲什麼這個答案被降低了投票?設置'textColor'屬性的確會改變'PagerTabStrip'中文本的顏色。 –

+5

它實際上工作!但我不知道爲什麼android提示沒有顯示這個特定的選項 –

10

獲取PagerTabStrip的子視圖並檢查它是否是TextView的實例。如果是,請設置文字顏色:

PagerTabStrip mPagerTabStrip = (PagerTabStrip) findViewById(R.id.pgstrip); 
for (int i = 0; i < mPagerTabStrip.getChildCount(); ++i) { 
    View nextChild = mPagerTabStrip.getChildAt(i); 
    if (nextChild instanceof TextView) { 
     TextView textViewToConvert = (TextView) nextChild; 
     textViewToConvert.setTextColor(getResources().getColor(R.color.primary_text_color_dark_gray)); 
    } 
} 
+2

如果對代碼有一點解釋,我會加註。 – Amicable

+3

我更新了我的答案。 –

相關問題