【问题标题】:Why the tint color is applied to other icons?为什么将色调颜色应用于其他图标?
【发布时间】:2021-05-16 08:17:32
【问题描述】:

考虑这个简单的布局:

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

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="0dp"
                app:navigationIcon="@drawable/ic_arrow_left"
                app:navigationIconTint="@color/red"
                app:layout_collapseMode="none"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

            <com.google.android.material.textview.MaterialTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/toolbar"
                android:background="?attr/selectableItemBackground"
                android:drawableEnd="@drawable/ic_arrow_left"
                android:text="@string/my_string"/>

</androidx.constraintlayout.widget.ConstraintLayout>

还有两个问题:

  1. navigationIcon 和 navigationIconTint 不起作用。工具栏保持默认图标,默认返回按钮和默认颜色。

  2. 这很奇怪:因为android:drawableEnd 中的MaterialTextView 使用与工具栏相同的图标(ic_arrow_left),所以它变成了红色。即使此处未指定色调,工具栏中使用的相同图标似乎也会应用于 TextView。对我来说这似乎是一个错误。

注意:关于 1 我应该提到我正在使用导航组件,并且后退图标是由导航组件本身自动生成的。

【问题讨论】:

    标签: android material-design toolbar android-toolbar material-components-android


    【解决方案1】:

    由于您使用的是导航组件,您可以使用 addOnDestinationChangedListener 来设置您的自定义图标和色调:

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.xxx){
            toolbar.setNavigationIcon(R.drawable.xxxx)
            toolbar.setNavigationIconTint(ContextCompat.getColor(this,R.color.xxx))
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 2023-01-26
      • 2012-10-10
      • 2012-08-10
      • 2021-11-24
      • 2017-08-10
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多