AppCompat工具栏暗色主题样式在21版本以下的设备上无法正常工作

4
首先,我已经反复阅读并尝试了这里发布的所有内容,但它没有解决我的问题。似乎这里发布的每个答案都是将样式应用于工具栏的主题属性,我已经尝试了,但却没有任何效果,所以让我解释一下我的问题:
我想在除了工具栏之外的地方都使用浅色主题。在Lollipop设备上,这不是问题,但在早期的Android设备上,工具栏标题和溢出按钮总是采用MyTheme父样式,因此我得到了深色标题和深色溢出按钮(那是我唯一的按钮),似乎在工具栏中,主题属性出现了故障。
我使用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);
}

你尝试过使用 android:theme 吗? - ianhanniballake
尝试在早期和Lollipop设备上使用app:theme,它应该可以正常工作。 - KudzieChase
"app" 只是 "..res-auto" 的自定义名称,我称之为 "local"(Mvx 标准)。因此,我将 "app:theme" 替换为 "local:theme"。它们的意思是相同的。 - ozapa
android:theme也不起作用 - ozapa
2个回答

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

在您的工具栏代码中尝试此组合,它应该可以正常工作。


它对我不起作用。标题和溢出按钮仍然来自浅色方案。 - ozapa
你能发一张截图吗? - KudzieChase

1
这是最终对我起作用的内容:
<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不起作用。我不知道为什么,但现在我不关心了。这不是最优解,但它能够工作。

你可以使用 android:theme 代替 local:theme。最近的 appcompat 版本使用 android:theme 来进行本地视图主题设置,而不是 app:theme。但是,你仍然需要使用 app:popupTheme(在你的情况下是 local:popupTheme)来设置弹出菜单主题。 - Lorne Laliberte
非常感谢您的回答,它起作用了,我也遇到了同样的问题。 - artman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接