mar*_*dan 33 data-binding android
我的布局文件:
<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>
Run Code Online (Sandbox Code Playgroud)
我的绑定适配器方法:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
Error:(8) No resource identifier found for attribute 'fontName' in package 'com.example.databindingproject'
Run Code Online (Sandbox Code Playgroud)
按照https://developer.android.com/tools/data-binding/guide.html中的教程进行操作.我可能做错了什么想法?
Geo*_*unt 65
您必须使用数据绑定语法.它应该是:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
app:fontName='@{"Roboto-Regular.ttf"}'
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
Wol*_*ang 12
如果忘记结束大括号,也会发生同样的错误:
android:text="@{viewModel.foo"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9082 次 |
| 最近记录: |