2015-08-14 67 views
2

我動作條的標題是2個字,我想用2個顏色每個字這樣的形象: enter image description here如何改變動作條的部分顏色的Android

現在我真的使用這些代碼來改變文字顏色:

<!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:actionBarSize">45dp</item> 
     <item name="actionBarSize">45dp</item> 


     <item name="android:actionBarStyle">@style/MyActionBar</item> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
     <item name="android:windowActionBarOverlay">true</item> 
    </style> 


    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar"> 
     <item name="android:background">@color/actionbar</item> 
     <item name="background">@color/actionbar</item> 
     <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 
     <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 
     <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 
     <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 
    </style> 
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance"> 
     <item name="android:textColor">@color/white</item> 
     <item name="android:textSize">15sp</item> 
</style> 

但這些代碼改變了字的顏色,所以我該怎麼做?

+0

使用的工具欄,而不是操作欄 –

回答

3

嘗試把它放在你oncreate()

String text = "<font color='#F44336'>Your</font><font color='#FF9800'>Title</font>"; 

     getSupportActionBar().setTitle(Html.fromHtml(text)); 
+0

它工作完美,謝謝 –

1

爲您的字幕創建單獨的樣式。在底部 補充一點:

<style name="MyTheme.ActionBar.SubTitleTextStyle" parent="android:style/TextAppearance"> 
    <item name="android:textColor">@color/black</item> 
    <item name="android:textSize">12sp</item> 
</style> 

然後改變

<item name="@android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 

<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> 

<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item> 

<item name="subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item> 
0

與spannable

String part1 = "Actionbar "; 
    String part2 = "TITLE"; 
    SpannableString s = new SpannableString(part1 + part2); 
    s.setSpan(new ForegroundColorSpan(Color.RED), part1.length(), part1.length()+part2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

// Update the action bar title with the spannable instance 
    getActionBar().setTitle(s); 
+0

我試試吧,可惜的是強制關閉happend ... –

+0

@NimaKiyani你能指向強制關閉的地方在代碼? – Oleksandr