为 arrayadapter 设置自定义字体
Set custom typeface for arrayadapter
这是我的代码:
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");
如何为适配器设置它?
我已经通过覆盖数组适配器解决了这个问题。希望,它会对其他人有所帮助)
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;
}
};
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;
}
}
这是我的代码:
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");
如何为适配器设置它?
我已经通过覆盖数组适配器解决了这个问题。希望,它会对其他人有所帮助)
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;
}
};
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;
}
}