繁体   English   中英

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

[英]AppCompat toolbar dark theme style not working on pre 21 devices

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

除了工具栏,我希望除了主题外都有轻量级主题。 在Lollipop设备上,这不是问题,但在Lollipop设备上工具栏标题和溢出按钮总是采用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);
}
<?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不起作用。 我不知道为什么会这样,但现在我不在乎。 这不是最佳解决方案,但可行。

暂无
暂无

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

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