如何为Android ListView设置自定义字体?

7

我刚刚接触安卓并正在使用自定义字体来显示列表视图。我不知道如何在列表视图中使用字体,尝试了不同的示例但无法解决我的问题。以下是我的代码:

public class HomeScreen extends ListActivity {
    private static final int QUICK_START_INDEX =0;
    private static final int CUSTOM = 1;
    private static final int CALL_LIST = 2;
    private static final int CALENDAR = 3;
    private static final int TEMPLATES=4;
    private static final int USER_GUIDE = 5;
    static final String[] LIST = 
               new String[] { "QuickStart", "Custom", "CallList", "Calendar","Templates","UserGuide"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new MobileArrayAdapter(this, LIST));
   /*Typeface font = Typeface.createFromAsset(getAssets(), "earthkid.ttf");  */

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        /*Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();*/

        if(position==QUICK_START_INDEX){
            startActivity(new Intent(HomeScreen.this,ConfDialer.class));
        }

    }

}

MobileArrayAdapter.java

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_home, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_home, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);


        // Change icon based on name
        String s = values[position];

        System.out.println(s);

        if (s.equals("QuickStart")) {
            imageView.setImageResource(R.drawable.quick_strat);
        } else if (s.equals("Custom")) {
            imageView.setImageResource(R.drawable.customize);
        } else if (s.equals("CallList")) {
            imageView.setImageResource(R.drawable.call_list);
        } else if(s.equals("Calendar")){
            imageView.setImageResource(R.drawable.calendar);
        } else if(s.equals("Templates")){
            imageView.setImageResource(R.drawable.templates);
        }
        else{
            imageView.setImageResource(R.drawable.user_guide);
        }

        return rowView;
    }


}

有人能提供这个问题的解决方案吗?

你有解决方案了吗,还是仍然存在问题? - Developer
我找到了解决方案,但不是你的。 - Deepak
好的,好的,一切顺利...祝你一切顺利...!!!! - Developer
7个回答

4

试试这个...

当您在Activity中创建一个包含nametitle的SimpleAdapter时,您可以重写getView()方法。

//global declaration
ListAdapter adapter;

adapter = SimpleAdapter(getActivity(), your_list, R.layout.your_list_items, new String[]{"Name", "Title"}, new int[]{R.id.name, R.id.title}){ 


@Override
 public View getView(int position,  View convertView, ViewGroup parent) {
           v = convertView;
     if(v == null){
     LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     v = li.inflate(R.layout.interview_list_items, null);
     TextView titleText= (TextView) v.findViewById(R.id.title);
     TextView nameText= (TextView) v.findViewById(R.id.name);
     childFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Light.ttf");
     titleText.setTypeface(childFont);
    nameText.setTypeface(childFont);
    return super.getView(position, v, parent);  
    }                                   
};                      

或者像这样在适配器类中尝试它...
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private Typeface tf; 

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_home, values);
    this.context = context;
    this.values = values;
    this.tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); //initialize typeface here.
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_home, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);

    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setTypeface(tf); // set typeface here
    textView.setText(values[position]);


    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("QuickStart")) {
        imageView.setImageResource(R.drawable.quick_strat);
    } else if (s.equals("Custom")) {
        imageView.setImageResource(R.drawable.customize);
    } else if (s.equals("CallList")) {
        imageView.setImageResource(R.drawable.call_list);
    } else if(s.equals("Calendar")){
        imageView.setImageResource(R.drawable.calendar);
    } else if(s.equals("Templates")){
        imageView.setImageResource(R.drawable.templates);
    }
    else{
        imageView.setImageResource(R.drawable.user_guide);
    }

    return rowView;
}

}

应用程序目录结构

enter image description here


1
使用此代码
在您的MobileArrayAdapter中。
class MobileArrayAdapter extends ArrayAdapter<CharSequence>{
            Typeface tf; 
        public MobileArrayAdapter(Context context, String[] values,String FONT) {
                tf = Typeface.createFromAsset(context.getAssets(), FONT);
            }

从你所在的位置 调用 你的适配器。
listAdapter = new MobileArrayAdapter(this, R.layout.custom_list_text, R.array.arrayName, "name_of_font.ttf");

1
将自定义字体文件.ttf放在assets文件夹中,使用以下代码更改字体。
text.setTypeface(Typeface.createFromAsset(getAssets(), "/*path to your font file */"));

路径将是资产中的路径。如果文件在资产文件夹中,则只需使用带扩展名的文件名即可。
例如:
 text.setTypeface(Typeface.createFromAsset(getAssets(), "arial.ttf"));

1
你只需要更改这个布局 "R.layout.list_home" 中的字体即可。因为 list_home 是作为单行工作的布局。所以如果你改变它,也会影响到 listview 的颜色/文本/样式。

同意。我认为最好在xml文件中进行样式设置,而不是在Java代码中。在布局文件中,将 android:fontFamily="@font/my_font" 添加到TextView中。并确保 my_font.ttf(或其他名称)位于 res/font/ 文件夹中。 - ban-geoengineering

0

我在我的适配器类中使用这段代码,它工作正常:

public class MyAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Item> item;

    public MyAdapter(Context context, ArrayList<Item> item) {
        this.context = context;
        this.item = item;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public Object getItem(int i) {
        return item.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_header, viewGroup, false);
        }

        Typeface MyFont= Typeface.createFromAsset(context.getAssets(), "Arial.ttf");
        TextView txtTitle = view.findViewById(R.id.txtTitle);
        txtTitle.setTypeface(MyFont);
  }
}

0

这是我的自定义适配器,如何调用以及在我的调用活动中传递哪些参数。

public class CustomData extends ArrayAdapter<CharSequence>    
{
   Context context;
   int layoutResourceId;
   CharSequence data[] = null;
   Typeface tf;

   public CustomData(Context context, int layoutResourceId, CharSequence[] data, String FONT) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
       tf = Typeface.createFromAsset(context.getAssets(), FONT);
   }
}

-1

使用

setTypeface(Typeface)

在TextView对象中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接