【问题标题】:Change toolbar back arrow color更改工具栏后退箭头颜色
【发布时间】:2016-04-07 11:46:29
【问题描述】:

嗨。在上图中,您可以看到一个后退箭头和一个(部分)标题。我使用附加的 .xml 代码更改了标题颜色。但我也想将后退箭头着色为白色。

我在互联网上阅读了一些答案,但对于这么简单的问题,它们看起来太复杂了。

有什么简单的原因吗?

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:titleTextColor="@android:color/white"/>

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
//...

public class LoginActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
      //...
    }
    //...
}

【问题讨论】:

    标签: android material-design android-toolbar android-appcompat android-support-design


    【解决方案1】:

    您可以覆盖 Toolbar 中的主题。
    使用 Material Components 主题

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

    与:

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

    使用 AppCompat 主题

    <android.support.v7.widget.Toolbar
      android:theme="@style/myToolbarTheme" 
      ...
    >
    

    然后在你的主题中你可以定义colorControlNormal属性:

       <style name=""  parent="ThemeOverlay.AppCompat.Dark.ActionBar">
          ....
          <item name="colorControlNormal">@color/myColor</item>  
       </style>
    

    【讨论】:

      【解决方案2】:

      请注意您使用的主题。如果您从https://developer.android.com/training/appbar/setting-up 复制了您的工具栏代码,那么您有这个:

      <android.support.v7.widget.Toolbar
         android:id="@+id/my_toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
         android:elevation="4dp"
         android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
         app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
      

      根据@Gabriele 的回答(赞成),我必须取出android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 属性并将其放入styles.xml,然后像这样自定义colorControlNormal 属性:

      <style name="ToolBarTheme"  parent="ThemeOverlay.AppCompat.ActionBar">
          <item name="colorControlNormal">@color/white</item>
      </style>
      

      回到我的工具栏声明,我修改如下:

      <android.support.v7.widget.Toolbar
         ...
         android:theme="@style/ToolBarTheme"
         />
      

      干杯!

      【讨论】:

      • 终于搞定了。我的布局中没有定义任何工具栏组件,但使用了 supportactionbar。但是在我的主题中使用actionBarThemecolorControlNormal 后退按钮现在可以根据需要进行着色。
      【解决方案3】:

      对我来说,我正在使用自己的可绘制后退箭头。

      首先将其添加到您的工具栏

        <androidx.appcompat.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:title="Your Title"
          app:navigationIcon="@drawable/ic_back"
          app:popupTheme="@style/AppTheme.PopupOverlay"
          >
      </androidx.appcompat.widget.Toolbar>
      

      如你所见

      app:navigationIcon="@drawable/ic_back"
      

      是随心所欲改变箭头的关键,这是它的样子

      <vector xmlns:android="http://schemas.android.com/apk/res/android"
      android:height="24dp"
      android:width="24dp"
      android:viewportWidth="24"
      android:autoMirrored="true"
      android:viewportHeight="24">
      <path android:fillColor="#C87BB3" android:pathData="M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z" />
      

      它是一个向量

      【讨论】: