Подтвердить что ты не робот

Android Theme.AppCompat.Light с Dark Toolbar (для легкого текста)

У меня есть приложение, использующее Theme.AppCompat.Light, которое мне нужно использовать, так что у Диалогов есть легкая тема. Но это делает текст на моей панели инструментов черным, где я предпочел бы, чтобы он был белым. Я пробовал настроить конкретную панель инструментов app:theme на AppCompat.Dark.Actionbar, но не повезло... Я некоторое время искал и не мог найти конкретный вопрос и ответить на это.

Здесь мой default 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" />
4b9b3361

Ответ 1

Чтобы изменить цвет заголовка в 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

Ответ 2

Вы можете настроить панель инструментов самостоятельно, не нужно устанавливать текст по умолчанию, используйте свой собственный текст:

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

Ответ 3

В то время как пользовательский стиль может работать, библиотека 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 в качестве родителя.