Android数据绑定 - '找不到属性的资源标识符'

时间:2015-09-04 12:39:31

标签: android data-binding

我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

<TextView
      android:text="@string/hello_world"
      android:layout_width="wrap_content"
      app:fontName="Roboto-Regular.ttf"
      android:layout_height="wrap_content"/>

</RelativeLayout>

我的绑定适配器方法:

public class FontBinding {

  @BindingAdapter("bind:fontName")
  public static void setFontName(TextView view, @NonNull String fontName) {
    String fontPath = "/fonts/" + fontName;

    Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), fontPath);

    view.setTypeface(typeface);
  }
}

我得到的错误:

Error:(8) No resource identifier found for attribute 'fontName' in package 'com.example.databindingproject'

遵循https://developer.android.com/tools/data-binding/guide.html的教程。 我可能做错了什么想法?

5 个答案:

答案 0 :(得分:55)

您必须使用数据绑定语法。它应该是:

<TextView
      android:text="@string/hello_world"
      android:layout_width="wrap_content"
      app:fontName='@{"Roboto-Regular.ttf"}'
      android:layout_height="wrap_content"/>

答案 1 :(得分:10)

如果忘记结束大括号,也会发生同样的错误:

android:text="@{viewModel.foo"

答案 2 :(得分:0)

如果要将 dp / px 值传递给@BindingAdapter,请记住使用绑定语法将其包装,即“ @ {}”

您需要使用dimens.xml传递值:

app:compoundDrawableSize="@{@dimen/icon_size}"

随附的绑定适配器使您能够为TextView设置copound可绘制的大小,如下所示:

/**
 * Binding adapter to set the size of TextView's compound drawable
 *
 * Usage:
 * <TextView   [...]
 *       android:drawableLeft="@{@drawable/ic_myicon}"
 *       app:compoundDrawableSize="@{@dimen/icon_size}"
 *  />
 */
@BindingAdapter("bind:compoundDrawableSize", "android:drawableLeft", "android:drawableStart",
        "android:drawableRight", "android:drawableEnd", requireAll = false)
fun TextView.setCompoundDrawableSize(px: Float, @IntegerRes drawableLeft: Drawable?, @IntegerRes drawableStart: Drawable?,
                                     @IntegerRes drawableRight: Drawable?, @IntegerRes drawableEnd: Drawable?) {
    val compoundDrawableLeft = drawableLeft ?: drawableStart
    val compoundDrawableRight = drawableRight ?: drawableEnd
    compoundDrawableLeft?.setBounds(0, 0, px.toInt(), px.toInt())
    compoundDrawableRight?.setBounds(0, 0, px.toInt(), px.toInt())
    this.setCompoundDrawables(compoundDrawableLeft, null, compoundDrawableRight, null)
}

答案 3 :(得分:0)

here所示,要将您的应用配置为使用数据绑定,请将dataBinding元素添加到应用模块中的build.gradle文件中,如以下示例所示:

android {
    ...
    dataBinding {
        enabled = true
    }
}

答案 4 :(得分:0)

我知道为时已晚我做了所有的答案,但它对我不起作用,当我添加此代码时:

dataBinding {
    enabled = true
}

在 app 级文件夹中的 build.gradle 中,在 android 标签内,如下所示:

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.development.allanproject"
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    dataBinding {
        enabled = true
    }
}

我的问题已解决。