【问题标题】:Change TextColor / Background of a single ActionBar Menu Item programmatically以编程方式更改单个 ActionBar 菜单项的 TextColor / 背景
【发布时间】:2015-02-27 10:23:07
【问题描述】:

有没有办法通过代码改变单个 ActionBar 菜单项的 Text 颜色或背景颜色?

为了更清楚一点,actionBar 中只有一项应该有不同的颜色,其余的应该是原始颜色。

如果是这样,我究竟如何才能做到这一点。 谢谢。

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

在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>

并改变你的表现形式

 android:theme="@style/AppTheme"

希望这篇文章对你有所帮助:)

【讨论】:

  • 谢谢您的回复,但是我只需要更改一项的颜色,其余的应该是原来的颜色。
  • 不用抱歉,很高兴得到您的帮助。
【解决方案2】:

背景:

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

文本颜色:

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

我认为您可以同时使用答案 @user3515851 和我的答案。

【讨论】:

  • 如果您使用此代码,您必须为所有活动添加此行。所以这不是更好的方法:)
  • 看他对你说的话,他只想用这个只有一个项目,他可以用你我的,因为他只能为一个项目定义一个主题或者只是在他的项目上使用它..
  • 问题是我的。对不起:(
  • 我的回答对您有帮助吗?如果是,则将其标记为正确:)
【解决方案3】:

如果您在某些情况下为操作栏使用自定义布局会更好。

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

因此您可以自定义,因为它是像 RelativeLayout 这样的普通布局。

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2011-10-31
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多