代码之家  ›  专栏  ›  技术社区  ›  Ashkan Sarlak

使用Android-M数据绑定应用单行字体不起作用

  •  6
  • Ashkan Sarlak  · 技术社区  · 10 年前

    我正在尝试将一些自定义字体应用于 TextView 其中一行如中所述 a post by Lisa Wray 这个 文本框 是进入 RecyclerView

    我已经将数据绑定依赖项添加到了顶层构建文件中。

    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath "com.android.databinding:dataBinder:1.0-rc1"
    

    我还将插件应用于我的主模块:

    apply plugin: 'com.android.application'
    apply plugin: 'com.android.databinding'
    

    这是 item.xml 将添加到 回收者视图 .

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/tools">
    
        <data></data>
    
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="100dp"
            android:layout_height="130dp"
            android:layout_gravity="center"
            card_view:cardCornerRadius="2dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"/>
    
                <TextView
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:font="@{@string/font_yekan}"/>
    
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </layout>
    

    我添加了 layout 根元素和 app:font="@{@string/font_yekan}" 结合静态setter方法:

    @BindingAdapter({"bind:font"})
    public static void setFont(TextView textView, String fontName) {
        textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
    }
    

    应该会成功。但当我运行程序时,字体没有改变。 然而,当我删除上述静态方法时,我得到以下错误:

    找不到参数类型为java.lang.String的属性“app:font”的setter。

    因此,数据绑定框架已经识别了绑定内容,但setter方法没有被调用(日志不会打印输出)。

    这里有什么问题?

    2 回复  |  直到 10 年前
        1
  •  6
  •   PLNech    10 年前

    提供了上述布局和设置,假设如下:

    在您的 RecyclerView 适配器,您可以通过以下方式之一绑定视图:

    1. 在适配器类的onCreateViewHolder方法中

      @Override
      public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recycler_item,
                  parent, false);
          return new MyHolder(binding.getRoot());
      }
      
    2. 或在其onBindViewHolder方法中

          @Override
          public void onBindViewHolder(MyAdapter.MyHolder holder, int position) {
              DataBindingUtil.bind(holder.itemView);
              //...
          }
      

    资源设置后

    您的资产文件夹应与以下内容类似:

    assets folder

    字符串资源文件应具有字体的完整限定名称:

    <string name="kenyan">kenyan_rg.ttf</string>
    

    有了这一点,它应该会起作用(对我来说也是如此)

        2
  •  0
  •   praveen2034    7 年前
        Even i have the same problem below is my code
    
    
        @BindingAdapter({"bind:customFont"})
            public static void customFont(TextView textView, String fontName) {
    textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/"+fontName));
            }    
    
        <TextView
                    android:id="@+id/tv_book_stay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="35dp"
                    app:customFont="@{@string/roboto_bold}"
                    android:text="@{data.mob1BookYourStay}"
                    android:textSize="22sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/iv_logo" />
    
        <string name="roboto_bold">roboto_bold.ttf</string>
    
        DataBindingUtil.setContentView(this, R.layout.activity_home);