2013-11-23 33 views
-1

我正在開發一些應用程序。其中包含列表視圖,我想在BaseAdapter類來顯示自定義字體文本..我用這個代碼,但是當我使用它的文本消失如何在Android中使用自定義字體更改listview中文本的字體?

TextView text = (TextView) vi.findViewById(R.id.text); // title 

tf = Typeface.createFromAsset(activity.getAssets(), "fonts/Vijaya.ttf"); 
text.setTypeface(tf); 
text.setText(quote.get(QuotesActivity.KEY_TEXT)); 

這是我的全碼:

類:

package com.engahmedphp.successquotes; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Typeface; 

import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class LazyQuotesAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater = null; 
    Context context; 
    Typeface tf; 
    public LazyQuotesAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     context = a; 

     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    // ============================================================================== 

    public int getCount() { 
     return data.size(); 
    } 

    // ============================================================================== 

    public Object getItem(int position) { 
     return position; 
    } 

    // ============================================================================== 

    public long getItemId(int position) { 
     return position; 
    } 

    // ============================================================================== 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.quote_list_row, null); 

     TextView text = (TextView) vi.findViewById(R.id.text); // title 

     tf = Typeface.createFromAsset(activity.getAssets(), "fonts/Vijaya.ttf"); 
     text.setTypeface(tf); 
     TextView author = (TextView) vi.findViewById(R.id.author); // category 
     ImageView picture = (ImageView) vi.findViewById(R.id.picture); // thumb 

     HashMap<String, String> quote = new HashMap<String, String>(); 
     quote = data.get(position); 

     // Setting all values in listview 

     text.setText(quote.get(QuotesActivity.KEY_TEXT)); 

     author.setText(quote.get(QuotesActivity.KEY_AUTHOR)); 

     // String picPath = Environment.getExternalStorageDirectory().getPath() 
     // + "/FacebookFeedsNotifier/" + quote.get(QuotesActivity.KEY_PICTURE); 
     // name.setText(picPath); 
     AssetManager assetManager = context.getAssets(); 
     InputStream istr = null; 
     try { 
      istr = assetManager.open("pictures/" + quote.get(QuotesActivity.KEY_PICTURE)); 
     } catch (IOException e) { 
      Log.e("assets", assetManager.toString()); 
      e.printStackTrace(); 
     } 
     Bitmap bmp = BitmapFactory.decodeStream(istr); 
     picture.setImageBitmap(bmp); 

     return vi; 
    } 
} 

視圖:

<TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:text="Rihanna Love the way lie" 
     android:textColor="#FFF" 
     android:textSize="18sp" 
     /> 

感謝提前:)

+0

你確定你把你的字體資產/字體dir在項目中? – CRUSADER

+0

是的,我確定:) –

+0

你確定文件名是Vijaya.ttf嗎? – zohreh

回答

0

試試這個..用它裏面你LazyQuotesAdapter構造

public LazyQuotesAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     context = a; 

     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     tf = Typeface.createFromAsset(context.getAssets(), "fonts/Vijaya.ttf"); 

    } 

OR

tf = Typeface.createFromAsset(vi.getContext().getAssets(), "fonts/Vijaya.ttf"); 
相關問題