无法为工具栏文本颜色提供文本



我的活动中有以下代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("My title");

styles.xml,我有这个:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    //more stuff
</style>
<style name="Toolbar" parent="Theme.AppCompat">
    <item name="android:textAppearance">@style/Toolbar.Text</item>
</style>
<style name="Toolbar.Text">
    <item name="android:textColor">@android:color/white</item>
</style>

和工具栏布局:

  <android.support.v7.widget.Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     app:layout_collapseMode="pin"
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
     app:theme="@style/Toolbar" />

我希望工具栏的文本是白色的,但它是黑色的。我尝试了很多东西,但我真的无法改变它。

不管是什么,问题来自这一行:

<style name="Toolbar" parent="Theme.AppCompat">

还有这个:app:theme="@style/Toolbar"(你用的是app而不是android,小心点)

而且你真的不需要这样做。


只需将这一行用于Toolbar

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

所以:

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme" />

然后你应该看到白色文本等

如您所见,我已将app:popupTheme设置为AppTheme参数为:

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

将该行添加到Toolbar后,现在您可以删除这些:

<style name="Toolbar" parent="Theme.AppCompat">
    <item name="android:textAppearance">@style/Toolbar.Text</item>
</style>
<style name="Toolbar.Text">
    <item name="android:textColor">@android:color/white</item>
</style>

然后,您唯一要做的就是将父级设置为app:popupTheme

Theme.AppCompat.Light.NoActionBar

它可以是你的Activity风格的父母。

就是这样:)

为此使用编辑器。转到工具->安卓->主题编辑器。在那里,您可以找到更改颜色的选项。

你可能使用的SDK少于23你可以试试这个

在 xml 中 :-

android:titleTextColor="@color/colorPrimary"

在片段中

 toolbar.setTitleTextColor(Color.RED);

最新更新