您可以从 ListViewAdapter 执行此操作。在我的项目中,我创建了一个扩展 ArrayAdapter 的新类:
class SummaryListAdapter extends ArrayAdapter<DynformSummary> {
static final int mViewResourceId = R.layout.dynformlist_item;
final Context mContext;
public SummaryListAdapter(Context context, DynformSummaryList items) {
super(context, mViewResourceId, items);
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(mViewResourceId, parent, false);
}
DynformSummary summary = getItem(position);
if (summary != null) {
TextView nameView = (TextView) view
.findViewById(R.id.dynformSummary_name);
TextView createdOnView = (TextView) view
.findViewById(R.id.dynformSummary_createdOn);
TextView itemSummaryView = (TextView) view
.findViewById(R.id.dynformSummary_itemSummary);
java.text.DateFormat df = DateFormat.getDateFormat(mContext);
String itemSummary = summary.getItemSummary();
if (itemSummary == null || itemSummary.length() == 0) {
itemSummary = mContext
.getString(R.string.placeholder_empty);
}
nameView.setText(summary.getName());
createdOnView.setText(df.format(summary.getCreatedOn()));
itemSummaryView.setText(itemSummary);
}
return view;
}
}
在您的情况下,您可以为每种不同的字体或颜色创建单独的布局 xml,或者您可以在运行时在 getView 方法中编辑字体/颜色。