更改工具栏返回箭头颜色

7

enter image description here

你好。在上面的图片中,你可以看到一个返回箭头和一个(部分)标题。我使用附带的.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);
      //...
    }
    //...
}

3个回答

22

您可以在工具栏中覆盖主题。
使用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>

6

请注意所使用的主题。 如果你从https://developer.android.com/training/appbar/setting-up复制了你的ToolBar代码,那么你可能有这个问题:

<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。但是在我的主题中使用“actionBarTheme”和“colorControlNormal”,现在返回按钮的颜色正如我想要的那样。 - A.W.

0

对于我来说,我正在使用自己的可绘制返回箭头。

首先将此添加到您的工具栏中

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

这是一个向量


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接