【问题标题】:How to change color of Toolbar back button in Android?如何在 Android 中更改工具栏后退按钮的颜色?
【发布时间】:2016-01-25 03:45:27
【问题描述】:

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

谢谢

【问题讨论】:

标签: android


【解决方案1】:

你可以在你的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: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>

【讨论】:

  • 效果很好,听起来最合适。
  • 该死。有用。现在我只需要与 iOS 等效的东西。谢谢!
  • 虽然称它为 ToolbarTheme 会更好
  • 对我来说
  • 成功了。这比接受的答案更合适。
【解决方案2】:

使用这种风格

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

【讨论】:

  • 我正在使用这种风格。这种风格我必须改变什么?
  • 您可以使用自定义图像文件作为后退按钮。只需在`@drawable/your_image'中调用它
  • 这是最好的方法。因为使用 R.drawable.abc_ic_ab_back_mtrl_am_alpha 改变颜色是有风险的,因为支持版本经常改变drawable资源。
【解决方案3】:

这是实现工具栏明暗主题的最简单方法。您必须更改工具栏标签的app:theme的值

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

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

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

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

【讨论】:

  • 对于白色工具栏标题和白色向上箭头,使用以下主题:android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  • 我想明白,为什么叫ThemeOverlay?为什么我不能在这里只使用简单的 Theme.AppCompat.Dark.ActionBar?
  • @ch3tanz 您使用的是操作栏还是工具栏?为什么要使用 ActionBar 样式?
  • @f470071 我正在使用工具栏。它只是将支持所有版本的样式。如果您的应用定位在 Lollipop 之上,则可以使用 Material Design 样式。
  • Theme.AppCompat.Light.DarkActionBar 在我的情况下。
【解决方案4】:

使用这个:

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

【讨论】:

  • 这应该被接受。这里的每个答案都只是替换图标,而不是颜色。
  • 这是整个列表中唯一有效的答案
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

    试试这个,

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

    【讨论】:

      【解决方案7】:

      只需使用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>
      

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

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

      【讨论】:

        【解决方案8】:

        您不必为它改变样式。将工具栏设置为操作栏后,您可以像这样编写代码

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

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

        【讨论】:

        • 非常感谢。这是唯一对我有用的解决方案!
        【解决方案9】:

        您可以使用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" />
        

        【讨论】:

        • 这对我有用,只需使用此行设置任何图标app:navigationIcon="@drawable/ic_back_black"
        • 庆幸它有帮助。
        • 对我没用。后退按钮或导航图标的颜色保持不变
        【解决方案10】:

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

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

        【讨论】:

        • 您的解决方案与@Parth Bhayani 有何不同?
        【解决方案11】:

        如果你想要白色的后退按钮 (←) 和白色的工具栏标题,请遵循:

        <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

        【讨论】:

          【解决方案12】:

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

          【讨论】:

            【解决方案13】:

            我是使用 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>
            

            【讨论】:

            • 这很有效:它允许您更改汉堡菜单图标的颜色。
            【解决方案14】:

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

            styles.xml

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

            【讨论】:

            • 对我不起作用
            • 白天不工作
            【解决方案15】:

            您可以将此代码添加到您的 java 类中。但是你必须先创建一个矢量资源,这样你才能自定义你的箭头。

            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
            

            【讨论】:

              【解决方案16】:

              简单无忧

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

              【讨论】:

                【解决方案17】:

                我们可以使用以下代码以编程方式实现。无需像许多其他人建议的那样添加我们自己的后退图标。

                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(),它将不起作用。

                【讨论】:

                  【解决方案18】:

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

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

                  【讨论】:

                    【解决方案19】:

                    如果您希望系统背景颜色为白色,则可以使用此代码

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-04-07
                      • 2015-04-28
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多