如何在android中使用自定义字体更改listview中文本的字体?

如何在android中使用自定义字体更改listview中文本的字体?,android,fonts,typeface,Android,Fonts,Typeface,我正在开发一些应用程序。其中包含listview,我想用自定义字体显示文本。。我在BaseAdapter类中使用此代码,但是当我使用它时,文本消失了 TextView text = (TextView) vi.findViewById(R.id.text); // title tf = Typeface.createFromAsset(activity.getAssets(), "fonts/Vijaya.ttf"); text.setTypeface(tf); text.setText(qu

我正在开发一些应用程序。其中包含listview,我想用自定义字体显示文本。。我在
BaseAdapter
类中使用此代码,但是当我使用它时,文本消失了

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"
       />
package com.engahmedphp.successquotes;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.HashMap;
导入android.app.Activity;
导入android.content.Context;
导入android.content.res.AssetManager;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Typeface;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类LazyQuotesAdapter扩展了BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
语境;
字体tf;
公共懒散配额适配器(活动a,阵列列表d){
活动=a;
数据=d;
上下文=a;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
// ==============================================================================
public int getCount(){
返回data.size();
}
// ==============================================================================
公共对象getItem(int位置){
返回位置;
}
// ==============================================================================
公共长getItemId(int位置){
返回位置;
}
// ==============================================================================
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.quote\u list\u行,空);
TextView text=(TextView)vi.findviewbyd(R.id.text);//title
tf=Typeface.createFromAsset(activity.getAssets(),“font/Vijaya.ttf”);
文本设置字体(tf);
TextView author=(TextView)vi.findviewbyd(R.id.author);//类别
ImageView图片=(ImageView)vi.findViewById(R.id.picture);//拇指
HashMap quote=新的HashMap();
quote=data.get(位置);
//在listview中设置所有值
text.setText(quote.get(quoteActivity.KEY_text));
author.setText(quote.get(quoteActivity.KEY_author));
//字符串picPath=Environment.getExternalStorageDirectory().getPath()
//+“/FacebookFeedsNotifier/”+quote.get(quoteActivity.KEY\u图片);
//name.setText(picPath);
AssetManager AssetManager=context.getAssets();
InputStream istr=null;
试一试{
istr=assetManager.open(“pictures/”+quote.get(quoteActivity.KEY_PICTURE));
}捕获(IOE异常){
Log.e(“资产”,assetManager.toString());
e、 printStackTrace();
}
位图bmp=BitmapFactory.decodeStream(istr);
picture.setImageBitmap(bmp);
返回vi;
}
}
查看:

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"
       />


提前感谢:)

试试这个。。在你的
lazyquoteAdapter
构造函数中使用它

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

    }

您确定将字体放在项目的资产/字体目录中吗?您确定文件名为Vijaya.ttf吗?