如何更改工具栏主页图标颜色
How to change Toolbar home icon color
我正在使用 android.support.v7.widget.Toolbar 并从 this post 学习如何将汉堡包图标的颜色更改为白色,但 up/back 箭头仍然是打电话时颜色深
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>
我通过编辑styles.xml解决了它:
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>
...然后在 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"/>
这就是您要查找的内容。但这也会改变 radioButton 等的颜色。所以你可能想为它使用一个主题。
<item name="colorControlNormal">@color/colorControlNormal</item>
这个答案可能为时已晚,但我是这样做的。
设计工具栏的样式就可以了。使用以下代码创建 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" />
我使用以下代码以编程方式解决了它:
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
。
<!-- 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>
来不及 post,这对我来说很有效,可以更改后退按钮的颜色
这段代码对我有用:
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 工作。
将您的工具栏主题更改为 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);
无需更改样式,只需将这两行代码添加到您的 activity。
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);
不要使用旧的 drawable id“abc_ic_ab_back_material”,而是使用新的 abc_ic_ab_back_material api 版本。我已经在 19、21、27 中对其进行了测试,并且可以使用以下代码和配置正常工作。
- minSdkVersion = 17
- targetSdkVersion = 26
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;
}
这是我的解决方案:
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)
试试这个:
在您的布局中设置工具栏的主题如下
android:theme = "@android:style/ThemeOverlay.Material.Dark.ActionBar"
如果您想了解更多信息
由于某种原因,无法使用此处的解决方案为其着色。但是使用 com.google.android.material.appbar.MaterialToolbar
你可以使用
app:navigationIconTint="@color/black"
参见:https://material.io/components/app-bars-top/android#regular-top-app-bar
我正在使用 android.support.v7.widget.Toolbar 并从 this post 学习如何将汉堡包图标的颜色更改为白色,但 up/back 箭头仍然是打电话时颜色深
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>
我通过编辑styles.xml解决了它:
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>
...然后在 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"/>
这就是您要查找的内容。但这也会改变 radioButton 等的颜色。所以你可能想为它使用一个主题。
<item name="colorControlNormal">@color/colorControlNormal</item>
这个答案可能为时已晚,但我是这样做的。 设计工具栏的样式就可以了。使用以下代码创建 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" />
我使用以下代码以编程方式解决了它:
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
。
<!-- 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>
来不及 post,这对我来说很有效,可以更改后退按钮的颜色
这段代码对我有用:
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 工作。
将您的工具栏主题更改为 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);
无需更改样式,只需将这两行代码添加到您的 activity。
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);
不要使用旧的 drawable id“abc_ic_ab_back_material”,而是使用新的 abc_ic_ab_back_material api 版本。我已经在 19、21、27 中对其进行了测试,并且可以使用以下代码和配置正常工作。
- minSdkVersion = 17
- targetSdkVersion = 26
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;
}
这是我的解决方案:
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)
试试这个: 在您的布局中设置工具栏的主题如下
android:theme = "@android:style/ThemeOverlay.Material.Dark.ActionBar"
如果您想了解更多信息
由于某种原因,无法使用此处的解决方案为其着色。但是使用 com.google.android.material.appbar.MaterialToolbar
你可以使用
app:navigationIconTint="@color/black"
参见:https://material.io/components/app-bars-top/android#regular-top-app-bar