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

Naz*_*MMF 74 android

我无法改变后退按钮的颜色.我正在使用工具栏材料设计.在我的应用程序中,我应用工具栏的黑色背景,但默认情况下背面设计为黑色,这就是为什么我只想更改此后退按钮的颜色.请给我解决方案.

谢谢

Raj*_*ela 160

您可以为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>
Run Code Online (Sandbox Code Playgroud)

并使用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>
Run Code Online (Sandbox Code Playgroud)

  • 有效。比接受的答案更合适。 (3认同)
  • 像魅力一样工作,听起来最合适。 (2认同)

ch3*_*anz 77

这是实现工具栏的浅色和深色主题的最简单方法.您必须更改app:theme工具栏标记的值

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

应用:主题= "@风格/ ThemeOverlay.AppCompat.Light"

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

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

应用:主题= "@风格/ ThemeOverlay.AppCompat"

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

  • 对于白色工具栏标题和白色向上箭头,请使用以下主题:android:theme ="@ style/ThemeOverlay.AppCompat.Dark.ActionBar" (58认同)
  • @AmolPatil的解决方案为我工作! (2认同)

Akh*_*mar 65

使用这种风格

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>
Run Code Online (Sandbox Code Playgroud)


小智 33

用这个:

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
</style>
Run Code Online (Sandbox Code Playgroud)

  • 这应该被接受。这里的每个答案只是替换图标,而不是颜色。 (7认同)

Pan*_*iya 25

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

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
Run Code Online (Sandbox Code Playgroud)


And*_*ree 22

试过上面的所有建议.我设法更改工具栏中导航图标默认后退按钮箭头的颜色的唯一方法是colorControlNormal在这样的基本主题中设置.可能是因为父母正在使用Theme.AppCompat.Light.NoActionBar

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
  //the rest of your codes...
</style>
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 11

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

    <com.google.android.material.appbar.MaterialToolbar
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        android:theme="@style/MyThemeOverlay_Toolbar"
        ..>
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <!-- color used by navigation icon and overflow icon -->
    <item name="colorOnPrimary">@color/...</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果您使用 具有默认样式 ( )的androidx.appcompat.widget.Toolbar或 ,您可以使用:MaterialToolbarWidget.MaterialComponents.Toolbar

<androidx.appcompat.widget.Toolbar
    android:theme="@style/MyThemeOverlay_Toolbar2"
Run Code Online (Sandbox Code Playgroud)

和:

  <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>
Run Code Online (Sandbox Code Playgroud)


Par*_*ani 10

试试这个,

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);
Run Code Online (Sandbox Code Playgroud)


Nev*_*ani 8

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

android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image
Run Code Online (Sandbox Code Playgroud)

但是你不能通过这种方法改变后箭头的颜色


nhp*_*nhp 6

如果您想要白色后退按钮 (?) 和白色工具栏标题,请按照以下步骤操作:

<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>
Run Code Online (Sandbox Code Playgroud)

如果您想要黑色后退按钮 (?) 和黑色工具栏标题DarkLight请将主题从 更改为。


moh*_*ge7 6

您可以通过使用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" />
Run Code Online (Sandbox Code Playgroud)


Sam*_*Sam 6

我使用材质组件库这样做了:

<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>
Run Code Online (Sandbox Code Playgroud)


And*_*ree 6

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

样式.xml

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/your_color</item> 
</style>
Run Code Online (Sandbox Code Playgroud)


Ped*_*mão 6

在 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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);
Run Code Online (Sandbox Code Playgroud)

  • 您的解决方案与@Parth Bhayani 有何不同? (2认同)