Android支持工具栏colorControlNormal颜色

时间:2014-10-23 20:59:30

标签: android android-actionbar android-5.0-lollipop android-actionbar-compat android-toolbar

我想将我的微调器颜色设置为白色,同时将我主题中的其他元素保持为默认颜色。情况如下:

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主题中(上面已注释掉),那么微调器下拉将会变为白色,但复选框也会变为白色。那么我怎样才能让微调器变成白色?

2 个答案:

答案 0 :(得分:17)

最后,我找到了一个如何将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
    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" />

答案 1 :(得分:12)

偶然发现了这些问题。即使前段时间有人问我,我只想留下一个应该有效的答案:

工具栏* .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>

<强>结果

solution with different colors for spinner and checkbox

注意:为了演示目的,我将复选框设为紫色