android - 如何为android listview设置自定义字体?

标签 android android-listview android-fonts

我是 android 的新手,正在为 ListView 使用自定义字体。我不知道如何在 ListView 中使用字体。我也尝试了不同的示例,但无法解决我的问题。这是我的代码

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

    }

}

移动阵列适配器.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;
    }


}

谁能为此提供解决方案。

最佳答案

试试这个...

您可以在 Activity 中创建适配器时覆盖 getView(),例如具有 nametitle 的 SimpleAdapter。

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

或者像这样在 Adapter 类中尝试...

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

关于android - 如何为android listview设置自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734273/

相关文章:

android - 在 ListView 标题按钮的 onclick 监听器中获取适配器引用

带有额外隐藏字段的 Android ListView

android - 为整个 Activity 设置字体设置

java - Dimens 文件中定义的字体大小,在 Java 类中显示 3 倍值

android - eclipse android logcat 显示一切

java - 找不到类 'dalvik.system....'

android - 停止 xml 实体生成正方形

php - 上传文件 - Android 到 Google App Engine (PHP)

Android:更改 ListView 的背景颜色

android.support.v7.widget.AppCompatCheckBox fontfamily 未在运行时应用