gpt4 book ai didi

android - 在 `ImageAdapter extends BaseAdapter` 中设置自定义字体

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:51 27 4
gpt4 key购买 nike

我正在使用 ImageAdapter extends BaseAdapter 来扩展 gridview。 Gridview 有两个 textview。我想为其中之一设置自定义字体。在 ImageAdapter 中使用 Typeface font = Typeface.createFromAsset(getAssets(), "BABYCAKE.TTF"); 给出错误 The method getAssets() is undefined for the type图片适配器.

ImageAdapter 定义为

package com.amit.wozzle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;

public class ImageAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<String> scorestage;
private Activity activity;
Typeface font;

public ImageAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<String> scorestage) {
super();
this.listCountry = listCountry;
this.scorestage = scorestage;
this.activity = activity;
font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}

@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewTitle2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();

if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_item, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.txtViewTitle2.setTypeface(font);
view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2);

// view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
// view.txtViewTitle2.setBackgroundColor(Color.BLUE);
view.txtViewTitle2.setText(listCountry.get(position));
view.txtViewTitle.setText(scorestage.get(position));
// view.imgViewFlag.setImageResource(scorestage.get(position));

return convertView;
}
}

请帮忙。

最佳答案

尝试

Typeface font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");

关于android - 在 `ImageAdapter extends BaseAdapter` 中设置自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11825540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com