Custom text font inside a Simplecursoradaptor

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:56:07

问题


I have made an application where i use a 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);

As you can see i show the three data at 2 TextViews (esodaTextView + amountTextView) and at one imageView.

The question is how can i set a custom font (that is saved at assets folder) to these 2 TextViews (esodaTextView + amountTextView) so i can show text with the custom font.

This is the xml file for the list item:

<?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>

I know that i can programmatically set a custom font via the following code but how can i use it inside a 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:


What you want to do is overwrite the SimpleCursorAdapter's 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;
    }
  });


来源:https://stackoverflow.com/questions/13541043/custom-text-font-inside-a-simplecursoradaptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!