How to change color of Toolbar back button in Android?

后端 未结 17 1114
不知归路
不知归路 2020-12-04 06:51

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 d

相关标签:
17条回答
  • 2020-12-04 07:25

    Just use the MaterialToolbar and override the default colors:

        <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.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>
    
    0 讨论(0)
  • 2020-12-04 07:28

    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);
    
    0 讨论(0)
  • 2020-12-04 07:29

    You can use your own icon by using app:navigationIcon and for the title color 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" />
    
    0 讨论(0)
  • 2020-12-04 07:31

    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);
    
    0 讨论(0)
  • 2020-12-04 07:33

    You can add a style to your 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

    <?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>
    
    0 讨论(0)
  • 2020-12-04 07:33

    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>
    
    0 讨论(0)
提交回复
热议问题