简体   繁体   English

带有Light App主题的深色工具栏

[英]Dark Toolbar with Light App Theme

I know this question has been asked before, and it worked for me before but after updating to App Compat revision 23, the Toolbar is now having a black text color (I want it white) and I didn't change a thing. 我知道之前曾有人问过这个问题,但之前对我有用。但是在更新到App Compat 23版之后,工具栏现在具有黑色文本颜色(我希望它是白色),但我没有做任何改动。

Toolbar.xml: Toolbar.xml:

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

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

styles.xml styles.xml

<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="@attr/spinner_style">@style/spinner_style</item>
</style>

Setting toolbar to activity: 将工具栏设置为活动:

 Toolbar Toolbar = (Toolbar) AppCompatActivity.findViewById(R.id.toolbar);
 AppCompatActivity.setSupportActionBar(Toolbar);
 ActionBar actionBar = ((AppCompatActivity)activity).getSupportActionBar();
 actionBar.setTitle(title);

Change 更改

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

to

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

The new support libraries (AppCompat) that Android Studio brings in default project templates do not insert an important attribute to style Toolbars in older Android versions. Android Studio引入默认项目模板的新支持库(AppCompat)不会在旧版Android中为样式工具栏插入重要属性。

This is what worked for me (I'm using an AppBarLayout with Toolbar and TabLayout, as shown): 这对我有用(我将AppBarLayout与Toolbar和TabLayout一起使用,如图所示):

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/AppTheme.AppBarOverlay" <!-- Needed attribute -->
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:theme="@style/AppTheme.AppBarOverlay" <!-- Needed attribute -->
            app:tabGravity="fill"
            app:tabMode="fixed" />

    </android.support.design.widget.AppBarLayout>

I didn't research to know exactly what happened here, but older Android versions really need the theme in every element to be styled. 我没有研究确切地知道这里发生了什么,但是较旧的Android版本确实需要在每个样式中设置主题。

Hope it helps someone who is trying to find a solution to the black text in the light toolbar theme! 希望它对试图在浅色工具栏主题中为黑色文本找到解决方案的人有所帮助!

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

相关问题 Android Theme.AppCompat.Light with Dark Toolbar(用于浅色文本) - Android Theme.AppCompat.Light with Dark Toolbar (for light text) 将android工具栏弹出菜单主题从黑暗变为光 - change android toolbar popup menu theme from dark to light 在应用程序处于深色/浅色主题(尤其是深色主题)时在 Flutter 中测试应用程序外观的最佳方法 - Best Approach To Testing App Appearance In Flutter While App Is In Dark/Light Theme (especially for Dark Theme) 错误:工具栏主题和深色主题 - Error: ToolBar theme and Dark theme 如何使应用程序具有黑暗和明亮的主题? - How can I make an app dark and light theme? 如何使用可组合物在应用程序中动态切换明暗主题 - How to switch between light and dark theme dynamically in app using composables 深色工具栏上的灯光操作模式 - Light action mode on dark toolbar 如果系统主题为浅色且应用主题为深色,则应用主题无法正确加载 - App theme doesn't load correctly if the system theme is light and the app theme is dark windowbackground 不适用于深色和浅色主题 - windowbackground not work in dark and light theme 查找主题的深色/浅色 - Find dark/light colors of theme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM