简体   繁体   English

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

[英]How to change color of Toolbar back button in Android?

I could not change the color of back button.我无法更改后退按钮的颜色。 I am using toolbar material design.我正在使用工具栏材料设计。 In my app I am applying black background of tool bar but the back design is being black by default that's why I just want to change the color of this back button.在我的应用程序中,我应用了工具栏的黑色背景,但默认情况下背面设计为黑色,这就是为什么我只想更改此后退按钮的颜色。 Please give me solutions.请给我解决方案。

Thank You谢谢你

You can add a style to your styles.xml,您可以在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>

and add this as theme to your toolbar in toolbar layout.xml using app:theme , check below并使用app:theme将此作为主题添加到您的工具栏 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>

use this style使用这种风格

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

Here is the simplest way of achieving Light and Dark Theme for Toolbar.You have to change the value of app:theme of the Toolbar tag这是实现工具栏明暗主题的最简单方法。您必须更改工具栏标签的app:theme的值

  • For Black Toolbar Title and Black Up arrow , your toolbar should implement following theme:对于黑色工具栏标题黑色向上箭头,您的工具栏应实现以下主题:

app:theme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Light"

黑色工具栏标题和向上箭头

  • For White Toolbar Title and White Up arrow , your toolbar should implement following theme:对于白色工具栏标题白色向上箭头,您的工具栏应实现以下主题:

app:theme="@style/ThemeOverlay.AppCompat" app:theme="@style/ThemeOverlay.AppCompat"

白色工具栏标题和向上箭头

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

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

Use this:用这个:

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

Tried all suggestions above. 尝试了以上所有建议。 The only way that I managed to change the color of navigation icon default Back button arrow in my toolbar is to set colorControlNormal in base theme like this. 我设法在工具栏中更改导航图标默认后退按钮箭头颜色的唯一方法是在基本主题中设置colorControlNormal ,如下所示。 Probably due to the fact that the parent is using Theme.AppCompat.Light.NoActionBar 可能是因为父级正在使用Theme.AppCompat.Light.NoActionBar

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
  //the rest of your codes...
</style>

Try this,尝试这个,

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);

Just use the MaterialToolbar and override the default colors:只需使用MaterialToolbar并覆盖默认颜色:

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

with:和:

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

在此处输入图片说明

If you are using the androidx.appcompat.widget.Toolbar or the MaterialToolbar with the default style ( Widget.MaterialComponents.Toolbar ) you can use:如果您使用androidx.appcompat.widget.ToolbarMaterialToolbar与默认样式( Widget.MaterialComponents.Toolbar ),您可以使用:

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

with:和:

  <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>

You don't have to change style for it.你不必为它改变风格。 After setting up your toolbar as actionbar, You can code like this将您的工具栏设置为操作栏后,您可以这样编码

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

But You cannot change color of back arrow by this method但是你不能通过这种方法改变后箭头的颜色

You can use your own icon by using app:navigationIcon and for the title color app:titleTextColor您可以使用app:navigationIcon和标题颜色app:titleTextColor使用自己的图标

Example:例子:

<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" />

I think if theme should be generated some problem but its dynamically set black arrow.So i Suggested try this one.我认为如果主题应该产生一些问题,但它动态设置的黑色箭头。所以我建议尝试这个。

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);

If you want white back button (←) and white toolbar title, follow this:如果你想要白色的后退按钮(←)和白色的工具栏标题,请按照以下步骤操作:

<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>

Change theme from Dark to Light if you want black back button (←) and black toolbar title.如果您想要黑色后退按钮 (←) 和黑色工具栏标题,请将主题从Dark更改为Light

To style the Toolbar on Android 21+ it's a bit different.在 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>

I did it this way using the Material Components library:我使用 Material Components 库这样做:

<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>

I am using <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar> theme in my android application and in NoActionBar theme, colorControlNormal property is used to change the color of navigation icon default Back button arrow in my toolbar我在我的 android 应用程序和 NoActionBar 主题中使用<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar>主题, colorControlNormal属性用于更改工具栏中导航图标默认后退按钮箭头的颜色

styles.xml样式文件

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

You can add this code into your java class.您可以将此代码添加到您的 java 类中。 But you must create a vector asset before, so you can customize your arrow back.但是您必须先创建一个矢量资产,以便您可以自定义返回箭头。

actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);

Simple and no Worries简单无后顾之忧

 <?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>

Programmatically we can achieve using below code.我们可以通过以下代码以编程方式实现。 No need to add our own back icon as suggested by many others.无需像许多其他人建议的那样添加我们自己的后退图标。

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));
        }
    }
}

Please note toolbar.getNavigationIcon() if requested before getSupportActionBar().setDisplayHomeAsUpEnabled(true);如果在getSupportActionBar().setDisplayHomeAsUpEnabled(true);之前请求,请注意toolbar.getNavigationIcon() getSupportActionBar().setDisplayHomeAsUpEnabled(true); it won't work.它不会工作。

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

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

I am using navigationIconTint property is used to change the color of navigation icon default Back button arrow in my toolbar我正在使用navigationIconTint属性用于更改工具栏中导航图标默认后退按钮箭头的颜色

styles.xml styles.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>

If you want system back color in white then you can use this code如果您希望系统背景颜色为白色,则可以使用此代码

<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>

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

The paste is in your activity where you want to show back button on action bar粘贴在您要在操作栏上显示后退按钮的活动中

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

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

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