如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。


好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?


当前回答

就我而言,我切换到AndroidX工具栏,而不是使用本机操作栏。此外,我依赖于视图绑定从活动访问工具栏,但您可以使用findViewById(…)。

(我删除了不相关的代码)。

styles.xml:

...
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  ...
</style>
...

activity_main.xml:

...
<androidx.appcompat.widget.Toolbar
  android:id="@+id/toolbarMain"
  android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
  app:titleTextColor="@color/colorOnPrimary"
  ...
/>
...

MainActivity.kt:

class MainActivity : AppCompatActivity() {

  private lateinit var binding: ActivityMainBinding
  ...

  override fun onCreate(savedInstanceState: Bundle?) {
    ...
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    setSupportActionBar(binding.toolbarMain)
    ...
  }

  ...
}

其他回答

最简单的方法是: 将这两行添加到styles.xml文件中

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColor">@color/white</item>
</style>

用你的颜色替换颜色。

将它添加到操作栏的根。我有这个问题。

<style name="ActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionMenuTextColor">@color/stdDarkBlueText</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

actionMenuTextColor -更改操作栏文本的颜色,当它是Widget.Styled的一部分时,它从未工作过。ActionBar,所以我必须把它添加到根目录中。其他2个属性更改操作栏的标题和副标题。

<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/color_primary"
  android:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
  app:popupTheme="@style/Theme.AppCompat.NoActionBar" />


<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
  <!-- android:textColorPrimary is the  color of the title text
       in the Toolbar, in the Theme.AppCompat theme:  -->
  <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
 
  <!-- android:textColorPrimaryInverse is the  color of the title
       text in the Toolbar, in the Theme.AppCompat.Light theme:  -->
  <!-- <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item> -->
 
  <!-- android:actionMenuTextColor is the color of the text of
        action (menu) items in the Toolbar, at least in the
        Theme.AppCompat theme.
        For some reason, they already get the textColorPrimary
        when running on API 21, but not on older versions of
        Android, so this is only necessary to support older
        Android versions.-->
        <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
  <!-- android:textColorSecondary is the color of the menu
       overflow icon (three vertical dots) -->
  <item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>
 
  <!-- This would set the toolbar's background color,
        but setting this also changes the popup menu's background,
        even if we define popupTheme for our <Toolbar> -->
  <!-- <item name="android:background">@color/color_primary</item> -->
</style>


**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]

我已经用简单的一行代码完成了

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));

好吧,我找到了一个更好的办法。我现在只能改变标题的颜色。你也可以调整副标题。

下面是我的styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>