在自定义arrayadapter中设置textview颜色

时间:2011-12-22 13:06:09

标签: java android android-arrayadapter

我正在尝试在自定义arrayadapter中设置TextView的颜色,但它不起作用 - 我得到的是灰色文本而不是绿色。

我尝试在row.xml中设置默认的textview颜色,但它也没有任何效果。

public class ReadCustomAdapter extends ArrayAdapter<ReadModel>{

    public ReadCustomAdapter(Activity a, int textViewResourceId, ArrayList<ReadModel> entries) {
        super(a, textViewResourceId, entries);
        this.entries = entries;
        this.activity = a;
    }

    public static class ViewHolder{
        public TextView item1;
        public TextView item2;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi =
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.item1 = (TextView) v.findViewById(android.R.id.text1);
            holder.item2 = (TextView) v.findViewById(android.R.id.text2);             
            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();

        final ReadModel custom = entries.get(position);

        if (custom != null) {
            holder.item1.setText(custom.getNick());
            holder.item2.setText(custom.getMsg());

            holder.item1.setTextColor(R.color.green);

        }
        return v;
    }

}

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="green">#00ff00</color>
</resources>

2 个答案:

答案 0 :(得分:2)

setTextColor()期待一个颜色整数(在你的情况下为0xFF00FF00),但是你为它提供了该颜色的资源ID,可以是任何数字。
您可以像这样使用它:

holder.item1.setTextColor(0xFF00FF00);

或者像这样:

holder.item1.setTextColor(getResources().getColor(R.color.green));

答案 1 :(得分:0)

你可以在values / colors.xml中设置@ color / my_value,就像这样;

@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule
],
declarations: [
LoginComponent,
ListComponent
],
providers: [
LoginService,
ListService,
LoginCredentials,
HeroActions,
HeroService
],
exports: [
LoginComponent,
ListComponent
]
})