android - Simplecursoradaptor 中的自定义文本字体

标签 android simplecursoradapter typeface custom-font

我制作了一个使用 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);

如您所见,我在 2 个 TextView(esodaTextView + amountTextView)和一个 imageView 中显示了三个数据。

问题是我如何为这 2 个 TextView (esodaTextView + amountTextView) 设置自定义字体(保存在 Assets 文件夹中),以便我可以使用自定义字体显示文本。

这是列表项的 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);

提前感谢您的帮助。

最佳答案

您要做的是覆盖 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;
    }
  });

关于android - Simplecursoradaptor 中的自定义文本字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13541043/

相关文章:

android,响应布局调整大小

android - 在 Xamarin Forms 应用程序中包含数千张图像

android - 是否可以将缩放控件移动到 OSMdroid 屏幕上的不同位置

android - ExpandableListView 的 SimpleCursorTreeAdapter 和 CursorLoader

android - 如何从数据库获取项目的值并将其设置为微调器值

android - Xamarin 为原生开发者构建跨平台 sdk

android - 将 recyclerview 与数据库一起使用

java - 在主线程中创建字体会影响应用程序性能吗?

android - 在 Android 对话框中设置自定义字体 (TypeFace)

android - 对具有 HTL 格式的相同 TextView 使用不同的字体