AppCompat 工具栏深色主题样式不适用于 21 之前的设备

AppCompat toolbar dark theme style not working on pre 21 devices

首先我已经反复阅读并应用了这里发布的所有内容,但它并没有解决我的问题。似乎这里发布的每个答案都是将样式应用于工具栏的主题属性,我已经试过了它让我无处可去,所以让我解释一下我的问题是什么:

我希望除工具栏外的所有地方都有浅色主题。在 Lollipop 设备上,这不是问题,但在 Lollipop 之前的设备上,工具栏标题和溢出按钮始终采用 MyTheme parent 样式,所以我得到深色标题和深色溢出按钮(这是我唯一的按钮)似乎是主题属性工具栏出现故障。

我正在为我的基础 class 使用 AppCompatActivity,我的最低 api 是 15,AppCompat 版本是 22.2.1.0

这是我的代码:

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



<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/primary_color</item> 
    <item name="colorPrimaryDark">@color/dark_primary_color</item> 
    <item name="colorAccent">@color/accent_color</item>
    <item name="android:statusBarColor">@color/dark_primary_color</item>
    <item name="selectableItemBackground">?android:attr/selectableItemBackground</item>
</style>

我的汉堡包是白色的,因为我从资源中膨胀图像

var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = "Sport";

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

 if (SupportActionBar != null){
 SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_white_24dp);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
</android.support.v7.widget.Toolbar>

在你的工具栏代码中试试这个组合,它应该工作得很好。

这最终对我有用:

<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:titleTextColor="@android:color/white"
    local:theme="@style/ToolbarTheme"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<style name="ToolbarTheme"  parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/primary_color</item>
    <!-- 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>
</style>

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_color</item> 
    <item name="colorPrimaryDark">@color/dark_primary_color</item> 
    <item name="colorAccent">@color/accent_color</item>
    <item name="android:statusBarColor">@color/dark_primary_color</item>
    <item name="colorControlNormal">#fff</item>
</style>

在本例中,ColorControlNormal 彩色溢出图标和 titleTextColor 彩色标题。似乎在我的情况下 ToolbarTheme 不起作用。我不知道为什么会这样,但现在我不在乎了。这不是最佳解决方案,但它有效。