我为一些微调器编写了一个自定义适配器,并为我的程序列出了视图。这是代码:
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 Builder。像这样:
SpannableStringBuilder textstring = new SpannableStringBuilder();
Aux.addText(textstring,cardInfo,Colors.White,Dim.TEXTSIZE_TEXTVIEW,Fonts.Normal);
text.setText(textstring, TextView.BufferType.SPANNABLE);
我想像这样编写适配器的每个项目。可以这样做吗?如果有人能告诉我怎么做的话,我会很乐意。
答案 0 :(得分:0)
以为我会回答我自己的问题,因为我找到了解决方案。关键是在构造函数中传递字符串列表,然后返回视图和下拉视图,并在相应的文本中添加了spannable字符串构建器。这是我的自定义适配器:
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;
private boolean setColor = false;
private List<String> data;
public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize, List<String> labels) {
super(context, resource,labels);
fontsize = fsize;
color = colour;
typeface = tf;
data = labels;
}
public void setBackgroundColor(int bg){
bgcolor = bg;
setColor = true;
}
public void setSelectedColor(int color){
selectedColor = color;
setColor = true;
}
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);
SpannableStringBuilder texter = new SpannableStringBuilder();
Aux.addText(texter,data.get(position),color,fontsize,typeface);
if (v != null) {
v.setText(texter, TextView.BufferType.SPANNABLE);
if (setColor) {
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);
SpannableStringBuilder texter = new SpannableStringBuilder();
Aux.addText(texter, data.get(position), color, fontsize, typeface);
if (v != null) {
v.setText(texter, TextView.BufferType.SPANNABLE);
if (setColor) {
if (position == selected) v.setBackgroundColor(selectedColor);
else v.setBackgroundColor(bgcolor);
}
}
return v;
}
}