简体   繁体   English

如何以编程方式和主题方式更改工具栏图标颜色?

[英]How to change toolbar icon color programmatically and theme way?

How to change toolbar icon color programmatically and theme way?如何以编程方式和主题方式更改工具栏图标颜色? Please suggest the best way to change the color请建议更改颜色的最佳方法

Programmatically you can change color like below以编程方式,您可以更改颜色,如下所示

Drawable drawable = toolbar.getNavigationIcon();
drawable.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);

Also, you can change this through the theme.此外,您可以通过主题更改此设置。

<style name="ToolbarColoreTheme" parent="AppTheme">
    <item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>

apply the above theme in your toolbar like below在您的工具栏中应用上述主题,如下所示

 <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        ***app:theme="@style/ToolbarColoreTheme"***
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

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

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