colorControlNormal未设置工具栏的后退箭头颜色

时间:2018-11-15 21:18:15

标签: android android-layout android-toolbar android-theme

我正在尝试将工具栏的向后箭头的颜色更改为紫色,但是为自定义工具栏主题的colorControlNormal属性设置的颜色不起作用。工具栏应为紫色,但仍为白色。

我已经检查了布局的xml和应用清单中的所有内容,以查看我的工具栏主题是否在某个地方被覆盖,但我看不到。

出什么问题了?

enter image description here

styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowContentTransitions">true</item>
        <item name="colorPrimary">@color/silver</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/roar_purple</item>
        <item name="actionMenuTextColor">@color/white</item>
        <item name="actionMenuTextAppearance">@style/customActionBar</item>
        <item name="android:textColorPrimary">@color/black_87_two</item>
        <item name="android:colorAccent">@color/roar_purple</item>
    </style>

    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
        <!-- Customize color of navigation drawer icon and back arrow -->
        <item name="colorControlNormal">@color/roar_purple</item>
        <item name="android:homeAsUpIndicator">@drawable/ic_chevron_left_white_48dp</item>
    </style>
...

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/phoneNumEntryAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ToolbarTheme"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
...

AndroidManifest.xml

<activity
    android:name=".LoginSignupEnterPhoneActivity"
    android:label="@string/title_activity_login_enter_phone_num"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" 
android:parentActivityName="com.roarforgood.roar.PrivacyDisclaimerActivity"
    android:theme="@style/AppTheme">
</activity>

2 个答案:

答案 0 :(得分:1)

文件名ic_chevron_left_white_48dp类似于您使用New -> Image AssetNew -> Vector Asset菜单选项时Android Studio创建的文件名。

如果您使用Image Asset,请删除所有文件,然后从Vector Asset重新开始。如果首先使用Vector Asset,请忽略此位。

当我生成白色人字形矢量时,它看起来像这样:

<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:tint="#FFFFFF"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

    <path
        android:fillColor="#FF000000"
        android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>

</vector>

要解决此问题,您需要做两件事:

  • android:tint标记中删除<vector>属性
  • android:fillColor标签中将?attr/colorControlNormal属性更改为<path>

那应该让你这个:

<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

    <path
        android:fillColor="?attr/colorControlNormal"
        android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>

</vector>

执行此操作并使用您发布的样式时,我会在工具栏中正确看到人字形的colorControlNormal

答案 1 :(得分:0)

注意:

 <item name="colorControlNormal">@color/roar_purple</item>
 <item name="android:homeAsUpIndicator">@drawable/ic_chevron_left_white_48dp</item> //delete this line

您只想显示后退箭头,因此只需执行以下操作:

 mToolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(mToolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setDisplayShowHomeEnabled(true);
相关问题