Simplecursoradapter内自定义文本字体

3

我制作了一个应用程序,其中使用了SimpleCursoradapter。

    String[] from = new String[] {"title","notes","image"};
    int[] to = new int[] { R.id.esodaTextView, R.id.amountTextView, R.id.imageView1}; 
    esodaAdapter = new SimpleCursorAdapter (this, R.layout.recipe_list_item, null, from, to);

您可以看到,我在两个TextView(esodaTextView + amountTextView)和一个imageView中展示了三个数据。

问题是如何为这两个TextView(esodaTextView + amountTextView)设置自定义字体(保存在资产文件夹中),以便可以显示带有自定义字体的文本。

这是列表项的xml文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="#55eee9e9" >

<TextView 
android:id="@+id/esodaTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20dp"
android:textColor="#000000"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" />

<TextView
android:id="@+id/amountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/esodaTextView"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:textSize="12dp" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

我知道可以通过以下代码编程设置自定义字体,但如何在Simplecursoradaptor中使用它?

 TextView txt1 = (TextView) findViewById(R.id.textView1);  
 Typeface font = Typeface.createFromAsset(getAssets(), "segoescb.ttf");  
 txt1.setTypeface(font);

Thank you for your help in advance.

1个回答

2
你想做的是覆盖SimpleCursorAdapter的ViewBinding
SimpleCursorAdapter.ViewBinder binding=adapter.setViewBinder( new SimpleCursorAdapter.ViewBinder()
  {
    boolean SetViewValue(View view, Cursor cursor, int columnIndex)
    {
      TextView tv=(TextView)view.findViewById(R.id.iesodaTextView)
      //Do what you want here
      return true;
    }
  });

在代码行:"TextView tv=view.findViewById(R.id.iesodaTextView);" 我遇到了一个错误:类型不匹配:无法从View转换为TextView。 - N.K.
1
修复了,只需要将 findViewById 的输出进行类型转换即可。 - PearsonArtPhoto
1
现在我没有收到错误消息,但是...文字未显示新字体...我已经在你的代码中添加了"return true;"。 - N.K.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接