-->

Android change ListView font

2019-05-15 08:46发布

问题:

Seems to be a simple question, but since I am new to Android development I have very little idea about Android ListViews. Following is the code I have used for a ListView in my project.

/*Listview code starts*/
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> planetList = new ArrayList<String>();  
planetList.addAll( Arrays.asList(values) );
listAdapter = new ArrayAdapter<String>(this, R.layout.list1, values);
mainListView.setAdapter(listAdapter); 
mainListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
                int itemPosition = position;
                String  itemValue    = (String) mainListView.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(),
                        "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                .show();
        }
});
/*Listview code ends*/

I wish to change the Font of my ListView text. How do I do that? Read everywhere to use a custom Adapter, but did not understand. Can anyone help me with the code?

回答1:

If you want to go by the way with creating a new ArrayAdapter and access to the items inside the ListView by overriding the getView() method. Please have a look at Adapter#getView .. Here and Here are good tutorials about customizing the ListView.

Sample custom ArrayAdapter will be like this.

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

    public CustomArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_mobile, 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_mobile, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);

        // Customization to your textView here
        textView.setText("Hello");
        textView.setTypeface(my_custom_typeface);
        textView.setTextSize(20);


        return rowView;
    }
}

And you can create a new CustomArrayAdapter by like this.

CustomArrayAdapter my_adapter = new CustomArrayAdapter();
setListAdapter(my_adapter);

Ref : Android TextView Methods.



回答2:

If you use adapter then write following line to your getView()..

 Typeface customfont=Typeface.createFromAsset(parent.getContext().getAssets(),"fonts/yourfont");

and then

 holder.tv.setText(actorList.get(position).getName());

 holder.tv.setTypeface(customfont);


回答3:

public class CustomAdapter extends ArrayAdapter {

private ArrayList<DataModel> dataSet;
Context mContext;


// View lookup cache
private static class ViewHolder {
    TextView txtName;
    CheckBox checkBox;
}

public CustomAdapter(ArrayList<DataModel> data, Context context) {
    super(context, R.layout.fathers_meet_text_check_layout, data);
    this.dataSet = data;
    this.mContext = context;

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

@Override
public DataModel getItem(int position) {
    return dataSet.get(position);
}


@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    final ViewHolder viewHolder;
    final View result;
    View v = convertView;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fathers_meet_text_check_layout, parent, false);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);


        result=convertView;
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    DataModel item = getItem(position);

    //use typeface
    Typeface customfont=Typeface.createFromAsset(parent.getContext().getAssets(),"VANAVILAvvaiyar.otf");

    viewHolder.txtName.setText(item.name);

    //set typeface
    viewHolder.txtName.setTypeface(customfont);
        viewHolder.checkBox.setChecked(item.checked);




    return result;
}

}