简体   繁体   English

如何更改工具栏主页图标颜色

[英]How to change Toolbar home icon color

I am using a android.support.v7.widget.Toolbar and learned from this post how to change the color of the hamburger icon to white, but the up/back arrow remains a dark color when I call我正在使用 android.support.v7.widget.Toolbar 并从这篇文章中学习了如何将汉堡图标的颜色更改为白色,但是当我调用时向上/向后箭头仍然是深色

setDisplayHomeAsUpEnabled(true);

How can I make the arrow white as well?我怎样才能使箭头也变白?

Here is what my toolbar looks like when I call setDisplayHomeAsUpEnabled():这是我调用 setDisplayHomeAsUpEnabled() 时工具栏的样子:

在此处输入图像描述

...and here is the relevant portion of my styles.xml file: ...这是我的 styles.xml 文件的相关部分:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">#194C5F</item>
    <item name="colorAccent">@color/accent</item>
    <item name="drawerArrowStyle">@style/WhiteDrawerIconStyle</item>
</style>

    <style name="WhiteDrawerIconStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

I solved it by editing styles.xml:我通过编辑styles.xml解决了它:

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

...then referencing the style in the Toolbar definition in the activity: ...然后在活动的工具栏定义中引用样式:

<LinearLayout
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <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/ToolbarColoredBackArrow"
        app:popupTheme="@style/AppTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

Here is what you are looking for.这就是你要找的东西。 But this also changes the color of radioButton etc. So you might want to use a theme for it.但这也会改变 radioButton 等的颜色。所以你可能想为它使用一个主题。

<item name="colorControlNormal">@color/colorControlNormal</item>

I solved it programmatically using this code:我使用以下代码以编程方式解决了它:

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

Revision 1:修订版 1:

Starting from API 23 (Marshmallow) the drawable resource abc_ic_ab_back_mtrl_am_alpha is changed to abc_ic_ab_back_material .从 API 23 (Marshmallow) 开始,可绘制资源abc_ic_ab_back_mtrl_am_alpha更改为abc_ic_ab_back_material

This answer maybe too late, but here is how I do it.这个答案可能为时已晚,但这是我的做法。 Styling the toolbar will do the trick.设置工具栏的样式就可以了。 Create toolbar.xml with following code.使用以下代码创建toolbar.xml。

<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_alignParentTop="true"
android:layout_gravity="bottom"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

and in the styles.xml在styles.xml中

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

Finally, include the toolbar inside layout最后,在布局中包含工具栏

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

Change your Toolbar Theme to ThemeOverlay.AppCompat.Dark将您的工具栏主题更改为 ThemeOverlay.AppCompat.Dark

<?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:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">

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

and set it in activty并将其设置为活动

mToolbar = (Toolbar) findViewById(R.id.navigation);
setSupportActionBar(mToolbar);

Here's my solution:这是我的解决方案:

toolbar.navigationIcon?.mutate()?.let {
  it.setTint(theColor)
  toolbar.navigationIcon = it
}

Or, if you want to use a nice function for it:或者,如果您想为此使用一个不错的功能:

fun Toolbar.setNavigationIconColor(@ColorInt color: Int) = navigationIcon?.mutate()?.let {
    it.setTint(color)
    this.navigationIcon = it
}

Usage:用法:

toolbar.setNavitationIconColor(someColor)
   <!-- ToolBar -->
    <style name="ToolBarTheme.ToolBarStyle"    parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorPrimaryInverse">@color/white</item>
    </style>

Too late to post, this worked for me to change the color of the back button太晚了,这对我改变后退按钮的颜色很有用

Well there is a more easy way to do this那么有一个更简单的方法来做到这一点

drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_drawer, R.string.close_drawer);
arrow = drawerToggle.getDrawerArrowDrawable();

And then接着

arrow.setColor(getResources().getColor(R.color.blue);

Instead of style changes, just put these two lines of code to your activity.无需更改样式,只需将这两行代码放入您的活动中。

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);

This code works for me:这段代码对我有用:

public static Drawable changeBackArrowColor(Context context, int color) {
    String resName;
    int res;

    resName = Build.VERSION.SDK_INT >= 23 ? "abc_ic_ab_back_material" : "abc_ic_ab_back_mtrl_am_alpha";
    res = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());

    final Drawable upArrow = context.getResources().getDrawable(res);
    upArrow.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

    return upArrow;
}

...

getSupportActionBar().setHomeAsUpIndicator(changeBackArrowColor(this, Color.rgb(50, 50, 50)));               
supportInvalidateOptionsMenu();

Also, if you want to change the toolbar text color:此外,如果要更改工具栏文本颜色:

Spannable spannableString = new SpannableString(t);
spannableString.setSpan(new ForegroundColorSpan(Color.rgb(50, 50, 50)), 0, t.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar.setText(spannableString);

Working from API 19 through 25.从 API 19 到 25 工作。

Try this: Set the toolbar's theme in your layout as follows试试这个:在布局中设置工具栏的主题,如下所示

android:theme = "@android:style/ThemeOverlay.Material.Dark.ActionBar"

If you want further information如果您想了解更多信息

The curious case of the Overflow Icon Color by Martin Bonnin Martin Bonnin 的溢出图标颜色的奇怪案例

Wasn't able to color it with the solutions here for some reason.由于某种原因,无法使用此处的解决方案对其进行着色。 But using com.google.android.material.appbar.MaterialToolbar instead you can use但是使用com.google.android.material.appbar.MaterialToolbar代替你可以使用

app:navigationIconTint="@color/black"

See: https://material.io/components/app-bars-top/android#regular-top-app-bar见: https ://material.io/components/app-bars-top/android#regular-top-app-bar

Instead of using older drawable id " abc_ic_ab_back_material ", use the new one abc_ic_ab_back_material in every api version.不要使用旧的可绘制 id“ abc_ic_ab_back_material ”,而是在每个 api 版本中使用新的abc_ic_ab_back_material I have tested it in 19, 21, 27 and working fine with below code and configuration.我已经在 19、21、27 中对其进行了测试,并且可以使用以下代码和配置正常工作。

  • minSdkVersion = 17 minSdkVersion = 17
  • targetSdkVersion = 26 targetSdkVersion = 26
  • compileSdkVersion = 27 compileSdkVersion = 27

     public static Drawable changeBackArrowColor(Context context, int color) { int res; res = context.getResources().getIdentifier("abc_ic_ab_back_material", "drawable", context.getPackageName()); if (res == 0) return null; final Drawable upArrow = ContextCompat.getDrawable(context, res); upArrow.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP); return upArrow;

    } }

This is best and easier option.这是最好和更容易的选择。 getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_24); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_24);

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

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