gpt4 book ai didi

属性 app :itemIconTint will be ignored 的 Android 数据绑定(bind)应用程序命名空间

转载 作者:太空狗 更新时间:2023-10-29 13:53:03 25 4
gpt4 key购买 nike

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

我有一个 BottomNavigationView,我想绑定(bind) itemIconTintitemTextColor 以根据 bool 值加载特定的 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>

这是 Utils 类:

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);
}
}

所以我的目标是根据 bool 值 lawyer 加载不同的 ColorStateListBottomNavigationView 将采用动态设计。

当我编译时我有这个错误:

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 enter image description here

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

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

最佳答案

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

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

但最终在您的情况下,您只需设置 app:itemTextColorapp:itemIconTintList 而无需定义适配器。处理器将选择正确的 setter setItemTextColor(ColorStateList)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" />

关于属性 app :itemIconTint will be ignored 的 Android 数据绑定(bind)应用程序命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43393163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com