为arrayadapter设置自定义字体

时间:2016-05-06 21:28:43

标签: android android-arrayadapter typeface

这是我的代码:

String[] filled_arr;
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.list_item, R.id.tv_wi, filled_arr);
listview.setAdapter(adapter);

这是字体:

final Typeface typeface = Typeface.createFromAsset(getAssets(), "Constantia.ttf");

如何为适配器设置它?

2 个答案:

答案 0 :(得分:5)

我已经通过覆盖数组适配器解决了这个问题。希望,它会帮助别人)

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_list_item_1, filled_arr) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTypeface(typeface);
            return view;
        }
    };

答案 1 :(得分:1)

public class CustomAdapter extends ArrayAdapter<DataModel> {

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