0
我已經爲我的程序編寫了幾個spinners和列表視圖的自定義適配器。下面是代碼:控制如何寫文本到Android上的spinners和列表
public static class CustomAdapter extends ArrayAdapter<String> {
private int fontsize;
private int color;
private Typeface typeface;
private int bgcolor = Color.rgb(50, 50, 50);
private int selectedColor = Color.rgb(50, 50, 50);
private int selected = -1;
public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize) {
super(context, resource);
fontsize = fsize;
color = colour;
typeface = tf;
}
public void setBackgroundColor(int bg){
bgcolor = bg;
}
public void setSelectedColor(int color){selectedColor = color;}
public void setSelectedPos(int p){
selected = p;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView)super.getView(position, convertView, parent);
v.setTypeface(typeface);
v.setTextSize(fontsize);
v.setTextColor(color);
if (position == selected) v.setBackgroundColor(selectedColor);
else v.setBackgroundColor(bgcolor);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView)super.getDropDownView(position, convertView, parent);
v.setTypeface(typeface);
v.setTextSize(fontsize);
v.setTextColor(color);
v.setBackgroundColor(bgcolor);
return v;
}
}
現在我想要做的就是控制如何文本在我返回文本視圖實際設置。更具體地說,我想使用SpannableString生成器,就像我在我的應用程序的其他普通文本視圖中一樣。像這樣:
SpannableStringBuilder textstring = new SpannableStringBuilder();
Aux.addText(textstring,cardInfo,Colors.White,Dim.TEXTSIZE_TEXTVIEW,Fonts.Normal);
text.setText(textstring, TextView.BufferType.SPANNABLE);
我想寫這樣的適配器的每個項目。這可以做到嗎?我會appreaciate如果有人可以告訴我如何。