如何与And​​roid中自定义字体的文字列表视图中改变字体? [英] how change font for text in listview with custom font in android?

查看:157
本文介绍了如何与And​​roid中自定义字体的文字列表视图中改变字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一些应用程序。其中包含列表视图,我想以显示与自定义字体文本..我用 BaseAdapter 类此code,但是当我用它的文本消失

I am developing some app. which contains listview and i want to display the text with custom font .. i am using this code in BaseAdapter class but when i use it the text is disappeared

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

这是我的全code:

类:

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

在此先感谢:)

推荐答案

试试这个。使用它里面你的 LazyQuotesAdapter 构造

Try this.. Use it Inside Your LazyQuotesAdapter constructor

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

    }

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

这篇关于如何与And​​roid中自定义字体的文字列表视图中改变字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆