简体   繁体   English

Android 数据绑定 - “未找到属性的资源标识符”

[英]Android data binding - 'No resource identifier found for attribute'

My layout file:我的布局文件:

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

My binding adapter method:我的绑定适配器方法:

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

The error I'm getting:我得到的错误:

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

Followed the tutorial from https://developer.android.com/tools/data-binding/guide.html .按照https://developer.android.com/tools/data-binding/guide.html 中的教程进行操作。 Any ideas of what I might be doing wrong?关于我可能做错了什么的任何想法?

You must use the data binding syntax.您必须使用数据绑定语法。 It should be:它应该是:

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

如果您忘记了右花括号,也会发生同样的错误:

android:text="@{viewModel.foo"

If you want to pass dp/px value to @BindingAdapter - remember to wrap it with a binding syntax ie "@{ }"如果您想将dp/px值传递给 @BindingAdapter - 请记住用绑定语法将它包装起来,即“@{ }”

You need to use dimens.xml to pass the value:您需要使用 dimens.xml 来传递值:

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

Accompanying binding adapter enabling you to set copound drawable size for a TextView would look like the following:随附的绑定适配器使您能够为 TextView 设置复合可绘制大小,如下所示:

/**
 * 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)
}

As shown here To configure your app to use data binding, add the dataBinding element to your build.gradle file in the app module, as shown in the following example:如此处所示要将您的应用程序配置为使用数据绑定,请将 dataBinding 元素添加到应用程序模块中的 build.gradle 文件中,如以下示例所示:

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

I know it is too late I did all the answers but it didn't work for me, and when I added this code:我知道为时已晚,我做了所有的答案,但它对我不起作用,当我添加此代码时:

dataBinding {
    enabled = true
}

in build.gradle in the app-level folder, inside the android tag like below:在 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
    }
}

My issue was fixed.我的问题已解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在包“ android”中找不到属性“ linearLayoutOrientation”的资源标识符 - No resource identifier found for attribute 'linearLayoutOrientation' in package 'android' 在'android'包中找不到属性'paddingEnd'的资源标识符 - No resource identifier found for attribute 'paddingEnd' in Package 'android' 在&#39;android&#39;包中找不到属性&#39;accessibilityEventTypes&#39;的资源标识符 - No resource identifier found for attribute 'accessibilityEventTypes' in package 'android' 在包“ android”中找不到属性“ depurables”的资源标识符 - No resource identifier found for attribute 'depurables' in package 'android' 在包“ android”中找不到属性“ domain”的资源标识符 - No resource identifier found for attribute 'domain' in package 'android' 在包android中找不到属性“ launchmode”的资源标识符 - no resource identifier found for attribute 'launchmode' in package android 在包“ android”中找不到属性“ fullBackupContent”的资源标识符 - No resource identifier found for attribute 'fullBackupContent' in package 'android' 在&#39;android&#39;包中找不到属性&#39;showAsAction&#39;的资源标识符 - No resource identifier found for attribute 'showAsAction' in package 'android' 在包“ android”中找不到属性“ qihoo”的资源标识符 - No resource identifier found for attribute 'qihoo' in package 'android' 在包Android Layout中找不到属性“”的资源标识符 - No resource identifier found for attribute '' in package Android Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM