如何在 Android 中改变工具栏后退按钮的颜色?

我无法改变后退按钮的颜色。我使用工具栏材质设计。在我的应用程序中,我应用了工具栏的黑色背景,但是背面的设计默认是黑色的,这就是为什么我只想改变这个背面按钮的颜色。请给我解决方案。

谢谢你

165071 次浏览

用这种风格

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>

试试这个,

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

您可以向 styles.xml 添加一个样式,

<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of navigation drawer icon and back arrow -->
<item name="colorControlNormal">@color/toolbar_color_control_normal</item>
</style>

并使用 App: 主题将其作为主题添加到工具栏 layout.xml 中的工具栏中,检查如下

<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ToolbarTheme" >
</android.support.v7.widget.Toolbar>

下面是实现工具栏的明暗主题的最简单方法。您必须更改 Toolbar 标记的 app:theme

  • 对于 黑色工具栏标题黑色向上箭头,您的工具栏应该实现以下主题:

主题 = “@style/ThemeOverlay. AppCompat. Light”

Black Toolbar Title and Up Arrow

  • 对于 白色工具栏标题白色向上箭头,您的工具栏应该实现以下主题:

主题 = “@style/ThemeOverlay. AppCompat”

White Toolbar Title and Up Arrow

我认为如果主题应该生成一些问题,但它的动态设置黑色箭头。所以我建议尝试这一个。

Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
backArrow.setColorFilter(getResources().getColor(R.color.md_grey_900), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);

对于白色工具栏标题和白色向上箭头,使用以下主题:

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

用这个:

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
</style>

如果你想要白色的后退按钮和白色的工具栏标题,按照下面的步骤:

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>

如果你想要黑色的后退按钮和黑色的工具栏标题,将主题从 Dark改为 Light

您可以使用您自己的图标通过使用 app:navigationIcon和标题颜色 app:titleTextColor

例如:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
app:titleTextColor="@color/white" />

简单,无忧无虑

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@color/white"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


</android.support.design.widget.AppBarLayout>


<include layout="@layout/testing" />


</android.support.design.widget.CoordinatorLayout>

你不必为它改变样式。在将你的工具栏设置为操作栏之后,你可以这样编码

android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image

但是您不能通过此方法更改反向箭头的颜色

如果你想白色的 系统返色系统返色系统返色系统返色系统返色系统,那么你可以使用这个代码

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">


<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_notification"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">


<include layout="@layout/action_bar_notification" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

注意:- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"是关键

粘贴在您的活动中,您希望在操作栏上显示返回按钮

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_notification);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);

我是这样使用材料组件库的:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>


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

我在我的 android 应用程序中使用 <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar>主题,在 NoActionBar 主题中,colorControlNormal属性用于改变我的工具栏中导航图标默认的后退按钮箭头的颜色

Xml

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/your_color</item>
</style>

您可以将此代码添加到 Java 类中。但是您必须先创建一个向量资产,这样才能自定义返回的箭头。

actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);

只需使用 MaterialToolbar并覆盖默认颜色:

    <com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:theme="@style/MyThemeOverlay_Toolbar"
..>

与:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- color used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/...</item>
</style>

enter image description here

如果您使用的是 androidx.appcompat.widget.ToolbarMaterialToolbar的默认样式(Widget.MaterialComponents.Toolbar) ,您可以使用:

<androidx.appcompat.widget.Toolbar
android:theme="@style/MyThemeOverlay_Toolbar2"

与:

  <style name="MyThemeOverlay_Toolbar2" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- This attributes is used by title -->
<item name="android:textColorPrimary">@color/white</item>


<!-- This attributes is used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/secondaryColor</item>
</style>

在 Android 21 + 上设计工具栏的风格有点不同。

<style name="DarkTheme.v21" parent="DarkTheme.v19">
<!-- toolbar background color -->
<item name="android:navigationBarColor">@color/color_primary_blue_dark</item>
<!-- toolbar back button color -->
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
</style>


<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">@color/color_white</item>
</style>

程序上我们可以实现使用下面的代码。没有必要添加我们自己的背部图标由许多其他人建议。

private void initToolBar(String title) {
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(title);
toolbar.setTitleTextColor(getResources().getColor(R.color.gold));
setSupportActionBar(toolbar);




// add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable nav = toolbar.getNavigationIcon();
if(nav != null) {
nav.setTint(getResources().getColor(R.color.gold));
}
}
}

如果在 getSupportActionBar().setDisplayHomeAsUpEnabled(true);之前提出要求,请注意 toolbar.getNavigationIcon(),它不会工作。

对于白色工具栏标题和白色向上箭头,使用以下主题:

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

我使用 navigationIconTint属性是用来改变颜色的导航图标默认返回按钮箭头在我的工具栏

Xml

<style name="AppTheme.ToolbarStyle" parent="Widget.MaterialComponents.Toolbar.Surface">
<item name="titleCentered">true</item>
<item name="titleTextColor">?attr/colorOnSurface</item>
<item name="navigationIconTint">?attr/colorOnSurface</item>
</style>