简体   繁体   English

Android Theme.AppCompat.Light with Dark Toolbar(用于浅色文本)

[英]Android Theme.AppCompat.Light with Dark Toolbar (for light text)

I have an app using Theme.AppCompat.Light which I need to use so Dialogs have the light theme.我有一个使用Theme.AppCompat.Light的应用程序,我需要使用它,以便对话框具有浅色主题。 But this makes the text in my toolbar black, where I'd rather it be white.但这会使我工具栏中的文本变黑,而我希望它是白色的。 I've tried setting the toolbar's specific app:theme to AppCompat.Dark.Actionbar , but no luck... I searched around for a while and couldn't find a specific question and answer to this.我已经尝试将工具栏的特定app:themeAppCompat.Dark.Actionbar ,但没有运气......我搜索了一段时间,找不到特定的问题和答案。

Here's my default AppTheme :这是我的默认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>

Here's my toolbar.xml :这是我的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" />

In order to change the color of the title in your Toolbar you simply have to add the attribute android:textColorPrimary to your Toolbar style .为了更改Toolbar标题的颜色,您只需将属性android:textColorPrimary添加到您的工具栏样式

Please see the following example for even more attributes of the Toolbar.有关工具栏的更多属性,请参见以下示例。

Example例子

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

Result结果

结果的图片

You can customize toolbar yourself no need to set default text, use your own textview :您可以自己自定义工具栏,无需设置默认文本,使用您自己的 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>

While custom styling may work, the AppCompat library already provides an easy way to do this.虽然自定义样式可能有效,但 AppCompat 库已经提供了一种简单的方法来做到这一点。 If your Toolbar 's background color is dark/doesn't provide enough contrast, use `ThemeOverlay.AppCompat.Dark as its parent:如果您的Toolbar的背景颜色很暗/没有提供足够的对比度,请使用`ThemeOverlay.AppCompat.Dark 作为其父级:

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

Then style your `Toolbar accordingly:然后相应地设置您的“工具栏”样式:

<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 s using backgrounds with lighter colors should use ThemeOverlay.AppCompat.Light as their parent.使用颜色较浅的背景的Toolbar应使用ThemeOverlay.AppCompat.Light作为其父级。

try this work in 2021, the first set of themes in the activity android:theme="@style/AppTheme.NoActionBar" and set theme in xml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM