Jos*_*a W 99 icons android toolbar android-support-library
我正在使用android.support.v7.widget.Toolbar并从这篇文章中了解如何将汉堡包图标的颜色更改为白色,但当我调用时,向上/向后箭头仍然是深色
setDisplayHomeAsUpEnabled(true);
我怎样才能使箭头变白?
当我调用setDisplayHomeAsUpEnabled()时,这是我的工具栏的样子:

...这是我的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>
Jos*_*a W 225
我通过编辑styles.xml解决了它:
<style name="ToolbarColoredBackArrow" parent="AppTheme">
    <item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>
...然后在活动中引用工具栏定义中的样式:
<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"/>
osr*_*srl 50
这是你要找的.但这也改变了radioButton等的颜色.所以你可能想要使用它的主题.
<item name="colorControlNormal">@color/colorControlNormal</item>
Pay*_*Pwn 35
我使用此代码以编程方式解决了它:
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);
修订版1:
从API 23(Marshmallow)开始,可绘制资源abc_ic_ab_back_mtrl_am_alpha更改为abc_ic_ab_back_material.
Tus*_*uss 14
这个答案可能为时已晚,但这就是我如何做到的.样式化工具栏将起到作用.使用以下代码创建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" />
并在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>
最后,在工具栏内部包含布局
<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
这是我的解决方案:
toolbar.navigationIcon?.mutate()?.let {
  it.setTint(theColor)
  toolbar.navigationIcon = it
}
或者,如果你想为它使用一个不错的函数:
fun Toolbar.setNavigationIconColor(@ColorInt color: Int) = navigationIcon?.mutate()?.let {
    it.setTint(color)
    this.navigationIcon = it
}
用法:
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>
发布太晚了,这对我有用,可以改变后退按钮的颜色
将工具栏主题更改为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>
并将其设置为活动状态
mToolbar = (Toolbar) findViewById(R.id.navigation);
setSupportActionBar(mToolbar);
那么有一个更简单的方法来做到这一点
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_drawer, R.string.close_drawer);
arrow = drawerToggle.getDrawerArrowDrawable();
然后
arrow.setColor(getResources().getColor(R.color.blue);
小智 7
无需更改样式,只需将这两行代码添加到您的活动中即可。
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);
由于某种原因无法使用此处的解决方案为其着色。但是使用com.google.android.material.appbar.MaterialToolbar你可以使用
app:navigationIconTint="@color/black"
请参阅:https://material.io/components/app-bars-top/android#regular-top-app-bar
这段代码对我有用:
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();
另外,如果要更改工具栏文本颜色:
Spannable spannableString = new SpannableString(t);
spannableString.setSpan(new ForegroundColorSpan(Color.rgb(50, 50, 50)), 0, t.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar.setText(spannableString);
从 API 19 到 25 工作。
| 归档时间: | 
 | 
| 查看次数: | 116358 次 | 
| 最近记录: |