Android 属性 app:itemIconTint 的数据绑定应用程序命名空间将被忽略

Android Data Binding Application namespace for attribute app:itemIconTint will be ignored

几个小时以来,我一直在解决数据绑定问题,特别是使用@BindingAdapter 进行自定义绑定。

我有一个 BottomNavigationView,我想绑定 itemIconTintitemTextColor 以根据布尔值加载特定的 ColorStateList。

所以我有以下 XML :

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context="mypackage.HomeActivity">

    <data>
        <variable
            name="utils"
            type="mypackage.model.Utils" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/content"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1"
            tools:layout_constraintTop_creator="1">

        </FrameLayout>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@{utils.lawyer ? @color/greyish_brown : @color/white}"
            app:itemIconTint="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}"
            app:itemTextColor="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1" />

    </android.support.constraint.ConstraintLayout>

</layout>

这是 class 实用程序:

public class Utils {

    private boolean lawyer;

    public boolean isLawyer() {
        return lawyer;
    }

    public void setLawyer(boolean lawyer) {
        this.lawyer = lawyer;
    }


    @BindingAdapter({"app:itemIconTint"})
    public static void setItemIconTint(BottomNavigationView view, ColorStateList colorStateList) {

        view.setItemIconTintList(colorStateList);
    }


    @BindingAdapter({"app:itemTextColor"})
    public static void setItemTextColor(BottomNavigationView view, ColorStateList colorStateList) {

        view.setItemTextColor(colorStateList);
    }
}

所以我的目标是根据布尔值 lawyer 加载不同的 ColorStateList。这样,BottomNavigationView 将具有动态设计。

当我编译时出现这个错误:

14:13:42.540 [ERROR] [system.err] Note: processing adapters
14:13:42.540 [ERROR] [system.err] Note: building generational class cache
14:13:42.540 [ERROR] [system.err] Note: loaded item android.databinding.annotationprocessor.ProcessBindable$IntermediateV1@49d08673 from file
14:13:42.540 [ERROR] [system.err] Note: loaded item android.databinding.annotationprocessor.ProcessExpressions$IntermediateV2@4e48888f from file
14:13:42.540 [ERROR] [system.err] Note: loaded item android.databinding.tool.store.SetterStore$IntermediateV3@11787d5f from file
14:13:42.540 [ERROR] [system.err] /Users/.../model/Utils.java:27: warning: Application namespace for attribute app:itemIconTint will be ignored.
14:13:42.540 [ERROR] [system.err]     public static void setItemIconTint(BottomNavigationView view, ColorStateList colorStateList) {
14:13:42.540 [ERROR] [system.err]                        ^
14:13:42.540 [ERROR] [system.err] Note: STORE addBindingAdapter itemIconTint setItemIconTint(android.support.design.widget.BottomNavigationView,int)
14:13:42.540 [ERROR] [system.err] Note: BINARY created method desc 2 mypackage.model.Utils setItemIconTint, setItemIconTint(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList)
14:13:42.540 [ERROR] [system.err] /Users/.../model/Utils.java:34: warning: Application namespace for attribute app:itemTextColor will be ignored.
14:13:42.540 [ERROR] [system.err]     public static void setItemTextColor(BottomNavigationView view, ColorStateList colorStateList) {
14:13:42.540 [ERROR] [system.err]                        ^
14:13:42.540 [ERROR] [system.err] Note: STORE addBindingAdapter itemTextColor setItemTextColor(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList)
14:13:42.540 [ERROR] [system.err] Note: BINARY created method desc 2 mypackage.Utils setItemTextColor, setItemTextColor(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList)

感谢您的帮助!

编辑:两个不同的颜色文件来自文件夹 Color

编辑 2:正如@tynn 所说,Android 数据绑定不知道资源类型。因此对于 Color 资源 @color 必须替换为 `@colorStateList。所以我做了更改,我的自定义 BindingAdapter 也已更新为接收 ColorStateList 而不是 int。问题是一样的

问题已解决:不需要自定义 BindingAdapter,因为数据绑定会根据每个 class 中现有的 setter 自动生成 setter(检查 Attribute Setters)。我的问题很奇怪,因为 R 文件,它生成得很糟糕。

您混淆了颜色和颜色状态列表。前者只是一个int,而后者应该是一个ColorStateList。在数据绑定 Expression Language 文档的末尾,您发现需要使用 @colorStateList 而不是 @color@color 也会产生颜色整数,而不是颜色资源。

对于您的适配器,您还必须使用不带命名空间的属性名称。

但最终在您的情况下,您只需设置 app:itemTextColorapp:itemIconTintList 而无需定义适配器。处理器将为您选择正确的设置器 setItemTextColor(ColorStateList) and setItemIconTintList(ColorStateList)

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@{utils.lawyer ? @color/greyish_brown : @color/white}"
    app:itemIconTintList="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}"
    app:itemTextColor="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}"
    app:menu="@menu/navigation" />