2012-11-24 44 views
3

我做了一個應用程序,我使用SimpleCursoradapter。Simplecursoradaptor內的自定義文本字體

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

正如你所看到的,我顯示了兩個TextViews(esodaTextView + amountTextView)和一個imageView的三個數據。

問題是如何設置自定義字體(即保存在assets文件夾中)到這兩個TextViews(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); 

感謝您提前給予的幫助。

回答

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; 
    } 
    }); 
+0

在代碼行: 「TextView的電視= view.findViewById(R.id.iesodaTextView);」我得到一個錯誤:類型不匹配:無法從視圖轉換爲TextView –

+1

修復,只需要從'findViewById'類型輸出。 – PearsonArtPhoto

+1

現在我沒有收到錯誤信息,但是......文字沒有顯示新的字體......我添加了「返回true」在你的代碼。 –