簡體   English   中英

使用Android-M數據綁定應用單行字體不起作用

[英]One-line font applying using Android-M data-binding doesn't work

我正在嘗試將一些自定義字體應用於我的TextView ,如Lisa Wray的帖子中所描述的那樣。 TextView是進入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'

這是將添加到RecyclerViewitem.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方法沒有被調用(Logs不打印輸出)。

這里有什么問題?

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

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

資源設置

您的資源文件夾應類似於:

資產文件夾

您的字符串資源文件應具有字體的完全限定名稱:

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

有了這個確保,它應該工作(它對我來說)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM