Tint CheckBox and Toolbar stuff separately

天涯浪子 提交于 2020-01-14 18:48:51

问题


I'm currently using the usually amazing appcompat-v7 library in my application and I'm trying to tint my CheckBoxes without changing the Icon-Colors in the Toolbar (like the back arrow or those three dots).

Following how it looks currently - as you can see not only the CheckBox is tinted pink, the back-arrow / three-dots are also tinted pink.

My code:

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        android:elevation="4dp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="My CheckBox" />

</LinearLayout>

Application-Theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">#FFC107</item>
    <item name="colorPrimaryDark">#FFA000</item>
    <item name="android:textColorPrimary">#FFF</item>
    <item name="android:textColorSecondary">#FFF</item>
    <item name="colorControlNormal">#E91E63</item>
</style>

Question

What do I need to change so that the Checkbox gets pink and the Toolbar-stuff keeps being white?

Side-Note: I guess the main problem is that I simply can't find a page where every single appcompat attribute is described. It's basically just all-out trial and error since the names of those attributes are no clear indication.


回答1:


You're basically facing the same issue as described in this or this question.
So the only thing you need to do is to create a Style for your Toolbar which uses an other theme as the Activity.

Toolbar style / theme example

<style name="MyToolbarStyle">
    <!-- potential other attributes -->
    <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>

Add this Style to your Toolbar and your issue should be fixed (it is important not to use the theme directly):

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/MyToolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        android:elevation="4dp" />


来源:https://stackoverflow.com/questions/29502571/tint-checkbox-and-toolbar-stuff-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!