带有黑暗工具栏的Android Theme.AppCompat.Light(用于浅色文字)

时间:2014-12-18 16:24:36

标签: android android-appcompat material

我有一个使用Theme.AppCompat.Light的应用程序,我需要使用它,因此Dialogs具有轻量级主题。但这会使我的工具栏中的文字变黑,我宁愿它变成白色。我已经尝试将工具栏的特定app:theme设置为AppCompat.Dark.Actionbar,但没有运气......我搜索了一段时间,找不到具体问题并回答此问题。

这是我的默认 AppTheme

<style name="AppTheme.Parent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorAccent">@color/pink</item>

    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

这是我的 toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

4 个答案:

答案 0 :(得分:17)

要更改Toolbar中标题的颜色,您只需将属性android:textColorPrimary添加到工具栏样式

有关工具栏的更多属性,请参阅以下示例。

实施例

<style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Used to for the title of the Toolbar -->
    <item name="android:textColorPrimary">#fff</item>
    <!-- Used to for the title of the Toolbar when parent is Theme.AppCompat.Light -->
    <item name="android:textColorPrimaryInverse">#fff</item>
    <!-- Used to color the text of the action menu icons -->
    <item name="android:textColorSecondary">#fff</item>
    <!-- Used to color the overflow menu icon -->
    <item name="actionMenuTextColor">#fff</item>
    <!-- Color of the Toolbar -->
    <item name="android:background">#455a64</item>
</style>

结果

picture of the result

答案 1 :(得分:2)

您可以自己自定义工具栏,无需设置默认文本,使用您自己的textview:

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary"
   android:minHeight="@dimen/abc_action_bar_default_height_material">

   <!-- Below will add your text in the center of toolbar -->
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Your Text"
        android:textColor=#ff0000
        android:textStyle="bold"/>

</android.support.v7.widget.Toolbar>

答案 2 :(得分:0)

虽然自定义样式可能有效,但AppCompat库已经提供了一种简单的方法。如果您的Toolbar背景颜色较暗/没有提供足够的对比度,请使用`ThemeOverlay.AppCompat.Dark作为其父级:

<style name="MyTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" />

然后相应地设置你的`工具栏:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/some_dark_color"
    android:theme="@style/MyTheme.PopupOverlay" />
使用较浅颜色的背景的

Toolbar应使用ThemeOverlay.AppCompat.Light作为其父级。

答案 3 :(得分:0)

2021年试试这个作品,活动android:theme="@style/AppTheme.NoActionBar"中的第一套主题,并在xml中设置主题

 <style name="AppTheme.NoActionBar">
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    <item name="windowNoTitle">true</item>
</style>
相关问题