如何连续更改颜色

时间:2014-04-19 15:59:14

标签: android listview

如果列表视图中的颜色包含值M?

,如何更改颜色

这是我的显示适配器,现在我有所有黑线,但如果你在txt_turno M中看到一个值,我希望在播放模式中以红色突出显示相应的行。

public View getView(int pos, View child, ViewGroup parent) {
        Holder mHolder;
        LayoutInflater layoutInflater;
        if (child == null) {
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            child = layoutInflater.inflate(R.layout.row, null);
            mHolder = new Holder();
            mHolder.txt_giorni = (TextView) child.findViewById(R.id.txt_giorni);
            mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);

1 个答案:

答案 0 :(得分:-1)

使用adapter's getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View rowView = inflater.inflate(R.layout.mainmenu_item, parent, false);

//text in which you want to find "M"
TextView text = (TextView) rowView.findViewById(R.id.text);

if(text.getText().toString().contains("M"){
    rowView.setBackgroundResource(R.color.red);
}
return rowView;

}

你还可以在res / values / colors.xml中定义R.color.red,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>