带有自定义适配器的listview中的android更改列表项
我想更改listview中的背景色。 号码的背景应随状态而变化带有自定义适配器的listview中的android更改列表项,android,listview,colors,adapter,Android,Listview,Colors,Adapter,我想更改listview中的背景色。 号码的背景应随状态而变化 1 = red 2 = grey 3 = green 4 = yellow 这是来自我的自定义适配器的代码(不工作): 为什么它不起作用 int cc[] = {R.color.red,r.color.blue} 将getview方法中textview的背景设置为 number.setBackgroundColor(cc[position]); 试试这个;用这个替换你的代码 TextView number = (TextVie
1 = red
2 = grey
3 = green
4 = yellow
这是来自我的自定义适配器的代码(不工作):
为什么它不起作用
int cc[] = {R.color.red,r.color.blue}
将getview方法中textview的背景设置为
number.setBackgroundColor(cc[position]);
试试这个;用这个替换你的代码
TextView number = (TextView) rowView.findViewById(R.id.tv_number);
switch(Integer.parseInt(status[position])){
case 1: number.setBackgroundColor(Color.RED);
break;
case 2: number.setBackgroundColor(Color.GRAY);
break;
case 3: number.setBackgroundColor(Color.GREEN);
break;
case 4: number.setBackgroundColor(Color.YELLOW);
break;
default :
break;
}
状态函数返回什么?rowView是什么?不多说就有点难说了 其他一些建议: 您应该避免像这样硬编码值,最好在re/values/文件夹中使用colors.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary_white">#ffffff</color>
</resources>
以下是有关在listview中更改背景和设置行样式的教程:
试试这个-
TextView number = (TextView) rowView.findViewById(R.id.tv_number);
switch(position+1){
case 1: number.setBackgroundColor(Color.RED);
break;
case 2: number.setBackgroundColor(Color.GRAY);
break;
case 3: number.setBackgroundColor(Color.GREEN);
break;
case 4: number.setBackgroundColor(Color.YELLOW);
break;
default :
break;
}
在getView方法中使用此代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (position % 4) {
case 0:
convertView.setBackgroundColor(Color.RED);
break;
case 1:
convertView.setBackgroundColor(Color.GREY);
break;
case 2:
convertView.setBackgroundColor(Color.GREEN);
break;
case 3:
convertView.setBackgroundColor(Color.YELLOW);
break;
default:
break;
}
}
希望对您有所帮助……您的状态[位置]值是否正确? 您可以编写日志statemet来检查值或调试代码 如果您的值正确,请尝试下面的代码段
TextView number = (TextView) rowView.findViewById(R.id.tv_number);
switch(Integer.parseInt(status[position])){
case 1: number.setBackgroundColor(Color.parseColor("#FF0000"));
break;
case 2: number.setBackgroundColor(Color.parseColor("#4D4D4D"));
break;
case 3: number.setBackgroundColor(Color.parseColor("#00FF00"));
break;
case 4: number.setBackgroundColor(Color.parseColor("#FFFF00"));
break;
default :
break;
}
希望有帮助。当您为小部件设置背景时,您还需要将alpha通道添加到希望使用的颜色中,因为Android要求颜色的格式为#ARGB,例如
#AARRGGBB
,其中a是alpha通道,而RGB是红-绿-蓝
因此,在您的情况下,如果您想要透明(00前缀)alpha通道,那么您需要为情况1执行此操作:
number.setBackgroundColor(0x00ff0000);
或者,对于不透明通道(ff前缀),执行以下操作:
number.setBackgroundColor(0xffff0000);
什么是状态[]?您可以直接将位置传递给切换大小写。状态[position]=1,2,3或4要更改背景颜色您要更改listview背景或列表项背景吗?@rapha31:可能您缺少alpha通道信息,因为Android采用的格式是#ARGB,例如#aarggbb,其中A是alpha通道,RGB是红-绿-蓝。所以,在你的例子中,如果你想要透明的alpha通道,那么对于案例1,你应该使用
0x00ff0000
,或者对于不透明通道,你应该使用0xff0000
@rapha31:我已经根据我上面的评论发布了我的答案。如果答案是正确的,请勾选它,以便将来阅读您文章的其他人知道我的答案是正确的。谢谢。@rapha31但这里你要的是挫折背景色不是alpha,好吗
TextView number = (TextView) rowView.findViewById(R.id.tv_number);
switch(Integer.parseInt(status[position])){
case 1: number.setBackgroundColor(Color.parseColor("#FF0000"));
break;
case 2: number.setBackgroundColor(Color.parseColor("#4D4D4D"));
break;
case 3: number.setBackgroundColor(Color.parseColor("#00FF00"));
break;
case 4: number.setBackgroundColor(Color.parseColor("#FFFF00"));
break;
default :
break;
}
number.setBackgroundColor(0x00ff0000);
number.setBackgroundColor(0xffff0000);