[英]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)
}
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.