view.setBackgroundColor()在android中不起作用
我有两个列表view.setBackgroundColor()在android中不起作用,android,listview,Android,Listview,我有两个列表listArray和itemStateList listArray包含要在ListView中显示的文本,itemStateList包含要在ListView中设置背景的颜色名称。但是setBackgroundColor(Color.parseColor(#AAFF7F”)不起作用 这是密码 public void createAndSetAdapter(){ ArrayAdapter<String> adapter = new ArrayAdapter<&g
listArray
和itemStateList
listArray
包含要在ListView
中显示的文本,itemStateList
包含要在ListView
中设置背景的颜色名称。但是setBackgroundColor(Color.parseColor(#AAFF7F”)
不起作用
这是密码
public void createAndSetAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listArray);
listView.setAdapter(adapter);
for(int i = 0; i < listArray.size(); i++){
switch (itemStateList.get(i)){
case "Green_color":
listView.getAdapter().getView(i, null, listView).setBackgroundColor(Color.parseColor("#AAFF7F"));
break;
case "red_color":
listView.getAdapter().getView(i, null, listView).setBackgroundColor(Color.parseColor("#FFAAAA"));
break;
default:
break;
}
}
}
public void createAndSetAdapter(){
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,listArray);
setAdapter(适配器);
对于(int i=0;i
为了检查listView.getAdapter().getView(i,null,listView)
是否返回视图,我运行((TextView)listView.getAdapter().getView(i,null,listView)).getText.toString()
,它返回了文本。尝试使用以下方法:
listView.getAdapter().getView(i, null, listView).setBackgroundResource(R.color.black);
希望这有帮助 尝试使用以下方法:
listView.getAdapter().getView(i, null, listView).setBackgroundResource(R.color.black);
希望这有帮助 首先,像这样在drawable中创建一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_background_shape">
<stroke
android:width="1dp"
android:color="#ffffff" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<corners android:radius="5dp" />
<solid android:color="@color/Transparent80" />
</shape>
首先,像这样在drawable中创建一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_background_shape">
<stroke
android:width="1dp"
android:color="#ffffff" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<corners android:radius="5dp" />
<solid android:color="@color/Transparent80" />
</shape>
使用挫折背景。“这会有用的。”阿斯拉霍辛,请你解释一下。我只是在学安卓。不知道什么是setBackgroundRes.setBackgroundResource和setBackgroundColor都使用相同的api setBackgroundDrawable内部执行其任务。所以一个覆盖了另一个。因此,您将无法使用此api实现目标。您必须将setBackgroundResource与自定义绘图一起使用。因此,根据您想要的颜色创建一个背景可绘制的xml,并像下面这样使用它@AslamHossin,
setBackgroundResource(R.color.green\u color)
不工作。请参阅我的完整答案代码。使用setBackgroundRes。“这会有用的。”阿斯拉霍辛,请你解释一下。我只是在学安卓。不知道什么是setBackgroundRes.setBackgroundResource和setBackgroundColor都使用相同的api setBackgroundDrawable内部执行其任务。所以一个覆盖了另一个。因此,您将无法使用此api实现目标。您必须将setBackgroundResource与自定义绘图一起使用。因此,根据您想要的颜色创建一个背景可绘制的xml,并像下面这样使用它@AslamHossin,setBackgroundResource(R.color.green\u color)
不工作。请参阅我的完整答案代码。