【问题标题】:AppCompat toolbar dark theme style not working on pre 21 devicesAppCompat 工具栏深色主题样式不适用于 21 之前的设备
【发布时间】:2015-09-29 16:31:57
【问题描述】:

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

我想在除工具栏之外的所有地方都有浅色主题。在棒棒糖设备上,这不是问题,但在棒棒糖设备之前的工具栏上,标题和溢出按钮总是采用 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
  • 好吧,试试app:theme 用于棒棒糖前和棒棒糖设备,它应该可以正常工作
  • "app" 只是“..res-auto”的自定义名称,我称之为“本地”(Mvx 标准)。所以代替“应用程序:主题”我有“本地:主题”。是一样的。
  • android:theme 也不起作用

标签: android toolbar android-appcompat


【解决方案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>

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

【讨论】:

  • 它对我不起作用。标题和溢出按钮仍然来自灯光方案。
  • 可以发截图吗?
【解决方案2】:

这最终对我有用:

<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)作为弹出菜单主题。
  • 非常感谢您的回答,它成功了,我遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 2015-01-14
  • 2015-02-07
  • 1970-01-01
相关资源
最近更新 更多