Android:工具栏文本显示为黑色而不是白色

17

我的工具栏文本、返回箭头等都是黑色的,但我希望它们变成白色。
我该怎么做?
我的 styles.xml 文件像这样:

<resources>

    <style name="AppTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/windowBackground</item>
        <item name="android:textColor">@color/textColorPrimary</item>
        <item name="android:textStyle">normal</item>



    </style>


</resources>

Android清单代码片段:

 <application
        android:allowBackup="true"
        android:icon="@mipmap/hello"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

4个回答

27

定义工具栏的样式:

<style name="AppToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

将其设置到您的工具栏中:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/AppToolbar"
        android:minHeight="?attr/actionBarSize"/>

14

如果您使用ThemeOverlay.AppCompat.Dark.ActionBar样式来制作您的工具栏,那么文本将会是浅色的。请参见此答案了解更多信息。

输入图片描述

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

1

参考这里的答案

为了改变工具栏中标题的颜色,你只需要在工具栏样式中添加属性android:textColorPrimary


1
尝试在values文件夹或values-v21和其他values文件夹的themes.xml或styles.xml中使用这些项目。
<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColor</item>
        <item name="colorAccent">@color/primaryColor</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:navigationBarColor">@android:color/black</item>

        <item name="actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:actionMenuTextColor">@android:color/holo_blue_dark</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentTransitions">true</item>
        </style>

注意:请在API级别21以下移除这些项目。
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/black</item>
        <item name="android:windowContentTransitions">true</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接