以下是我的代码:
我想更改其id为'name12'的Textview的字体。需要帮忙。提前谢谢。
String rid = jObj.getString(TAG_RID);
String name = jObj.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_RID, rid);
map.put(TAG_NAME, name);
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_v, new String[] { TAG_NAME }, new int[] { R.id.name12});
l1.setAdapter(adapter);
答案 0 :(得分:1)
你可以extend TextView
分类并在里面设置字体。
之后,您可以在TextView
R.layout.list_v
public class TextViewWithFont extends TextView {
public TextViewWithFont(Context c) {
this(c, null);
}
public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeface(Typeface.createFromAsset(context.getAssets(), "fontname.ttf"));
}
public TextViewWithFont(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
}
答案 1 :(得分:0)
实现您想要的更好方法是创建自定义适配器。这将为您提供更多控制权。
如果您希望简单的适配器使用任何方式,请执行以下操作:
public class CustomAdapter extends SimpleAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Object item = getItem(position);
//iterate to this view and it's child and if it's an instance of textview set the color
ColorStateList color = //get color for item;
text.setTextColor(color);
return v;
}
}