我正在尝试在列表视图中设置条目的备用颜色。我尝试使用setBackgroundColor和setBackgroundResource,但我似乎只获得透明背景。
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflateView(R.layout.pending_list);
if(position % 2 != 0){
convertView.setBackgroundResource(R.drawable.list_item_odd);
} else {
convertView.setBackgroundResource(R.drawable.list_item_even);
}
return convertView;
}
}
这是list_item_odd.xml
<item android:state_pressed="true"><shape>
<solid android:color="@color/blue" />
</shape></item>
<item android:state_focused="true"><shape>
<solid android:color="@color/blue" />
</shape></item>
<item><shape>
<solid android:color="@color/bluish" />
</shape></item>
这是colors.xml文件
<resources xmlns:android="http://schemas.android.com/apk/res/android" >
<color name="blue">#006C9C</color>
<color name="white">#ffffff</color>
<color name="whitish">#FDFDFD</color>
<color name="bluish">#F2F9FD</color>
</resources>
我无法弄清楚为什么我会得到这个结果。
答案 0 :(得分:1)
这对我有用
private int[] listdrawable ={R.drawable.list_backgroung, R.drawable.list_backgroung1111}; //list of drawable background
int colorPos = position % listdrawable.length; //in getView() method before setting the values
convertView.setBackgroundResource(listdrawable[colorPos]);
reurn convertView;