在自定义ListAdapter上编辑字体

时间:2013-07-19 10:39:55

标签: android listview fonts custom-adapter typeface

我必须更改自定义列表适配器上的字体。 我有一个问题,因为我无法获得当前的背景。

getassets() not exist for the class
getApplicationcontext()  not exist for the class
getBaseContext()  not exist for the class

我正试图从视图中获取上下文,没有错误,但字体没有改变

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Context  context = convertView.getContext();
//or
       Context  context2 = parent.getContext();

在我写的代码之后

 bold = Typeface.createFromAsset(context.getAssets(), "fonts/Jennifer-Lynne.ttf");
        italic = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica-italic.ttf");
        holder.titoloView.setTypeface(bold);
        holder.autoreView.setTypeface(italic);

主意?

2 个答案:

答案 0 :(得分:0)

将Context传递给CustomListAdapter类构造函数,并使用它来从资产中获取字体。初始化ViewHolder视图时设置字体(当getView在getView中为null时)。

答案 1 :(得分:0)

这是正确的代码(资产文件夹上的字体)

全局变量

Typeface tf, bold, italic;
   Context prova;

我设置上下文的方法

public CopyOfCustomListAdapterLele(Context context, ArrayList listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        prova = context;
    }

和字体的设置

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;


        bold = Typeface.createFromAsset(prova.getAssets(), "fonts/Jennifer-Lynne.ttf");



        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.singolopunto, null);
            holder = new ViewHolder();
            holder.titoloView = (TextView) convertView.findViewById(R.id.textView1); //titolo canzone

            holder.imageView = (SmartImageView) convertView.findViewById(R.id.imageView2); //immagine
            holder.free = (ImageView) convertView.findViewById(R.id.imageView3); //immagine free

            holder.titoloView.setTypeface(bold);


...