简体   繁体   English

以编程方式更改单个ActionBar菜单项的TextColor / Background

[英]Change TextColor / Background of a single ActionBar Menu Item programmatically

Is there a way to change the Text color or background color of a single ActionBar menu item by code ? 有没有办法按代码更改单个ActionBar菜单项的文本颜色或背景颜色?

To be more clear, only one item of the actionBar should have a different color, the rest of them should be the original color. 更清楚的是,只有一个actionBar项应该有不同的颜色,其余的应该是原始颜色。

If so, how exactly can I achieve this. 如果是这样,我究竟能做到这一点。 Thank you. 谢谢。

add this lines at styles.xml 在styles.xml中添加这一行

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#de5188</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="android:subtitleTextStyle">@style/MyActionBarSubTitleText</item>
</style>

<style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="MyActionBarSubTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">10dp</item>
</style>

And change your manifasted as 并改变你的表现

 android:theme="@style/AppTheme"

I hope this one help to you :) 我希望这个对你有帮助:)

Background : 背景 :

 ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); //<-- Your Color

TextColor : TextColor:

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

I think you can use both answer @user3515851 and mine. 我想你可以同时使用answer @ user3515851和我的。

It would be better if you used a custom Layout for action bar for some cases like this. 如果您在某些情况下使用自定义布局操作栏会更好。

actionBar.showCustomView(true)// or similar to this.
actionBar.setCustomView(view)// you can inflate the view as you wish

so you can customise as it was a normal layout like RelativeLayout. 所以你可以自定义,因为它是像RelativeLayout这样的普通布局。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM