Android支持工具栏colorControlNormal颜色

23
我想将我的下拉菜单(spinner)的颜色设置为白色,同时保持我的主题中其他元素的默认颜色。情况如下所示:

Toolbar and spinner

<android.support.v7.widget.Toolbar
        android:layout_height="@dimen/action_bar_height"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ToolbarTheme.Base"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"/>

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

而且风格是:

<!-- My base app theme -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/theme_tint</item>

    <!-- <item name="colorControlNormal">#FFFFFF</item> -->

    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:typeface">sans</item>
    <item name="android:windowBackground">@color/background_primary</item>
</style>

<!-- My Toolbar theme -->
<style name="ToolbarTheme.Base" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlNormal">#FFFFFF</item>
</style>
如果我将<item name="colorControlNormal">#FFFFFF</item>放在App主题中(如上所述被注释掉),那么下拉菜单会变成白色,但复选框也会变成白色。那么我怎样才能使下拉菜单仅变成白色呢?

当您完全从ToolbarTheme.Base中删除colorControlNormal时会发生什么? - ianhanniballake
没有区别,旋转器仍然是灰色的。 - James Cross
我遇到了同样的问题,似乎在某些设备上可以工作而在其他设备上不行。https://dev59.com/1l8d5IYBdhLWcg3wgya4 - tyczj
2个回答

21

最终,我找到了一个解决方案,可以将 Spinner 的箭头颜色改为白色。

1)在 styles.xml 中添加以下样式:

<style name="ActionBarThemeOverlay" parent="">
    <item name="android:textColorPrimary">#ffffff</item>
    <item name="colorControlNormal">#ffffff</item>
    <item name="colorControlHighlight">#ff33b5e5</item>
</style>

<style name="Widget.MyTheme.HeaderBar.Spinner" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
    <item name="android:theme">@style/ActionBarThemeOverlay</item>
</style>

2) 在布局中,当您使用Spinner(在您的情况下与Toolbar一起使用)时,在您的 Spinner 中添加样式:

<Spinner
    xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/my_spinner"
         style="@style/Widget.MyTheme.HeaderBar.Spinner"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

谢谢你,我会尽快尝试一下。 - James Cross
我尝试了同样的方法,但是我无法看到旋转箭头上的任何变化,它仍然是白色的。我想把它改成黑色。你认为还有其他解决这个问题的方法吗? - Rajat kumar
@JamesCross,这有帮助到您吗? - yyunikov
好的,我看过很多教程和stackoverflow上的答案,这绝对是我认为最好解决这个问题的方法。 - nitrobuster

18

刚刚偶然看到这个问题。虽然它已经被问过一段时间了,但我仍想留下一个应该有效的答案:

工具栏 *.xml

<android.support.v7.widget.Toolbar
    <!-- leave the theme stuff out of here -->
    style="@style/MyToolbarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

样式/主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- your other attributes -->
    <!-- following is used to tint the checkbox - purple for demo purpose -->
    <item name="colorControlNormal">#2196F3</item>
</style>

<style name="MyToolbarStyle">
    <item name="android:minHeight">?actionBarSize</item>
    <item name="android:background">?colorPrimary</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/MyToolbarTheme</item>
</style>

<style name="MyToolbarTheme">
    <!-- Used to tint the back arrow, menu and spinner arrow -->
    <item name="colorControlNormal">#FFF</item>
</style>

结果

使用不同颜色的旋转器和复选框的解决方案

注意:我将复选框设置为紫色仅用于演示目的


虽然@yuriy-yunikov的答案通过android:theme设置Spinner的颜色非常好用,但这对我不起作用。 - Damian Walczak
请记住,在Spinner本身上使用style属性设置样式非常重要,因为通过应用程序主题设置它似乎无法传播Spinnertheme属性。 - Damian Walczak
@DamianWalczak的帖子有点老了,我得检查一下。但我认为我的方法仍然有效。此外,自从Appcompat v22以来,我认为您可以为每个视图元素设置“android:theme”,因此“style”内容也已过时。 - reVerse

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