Android ListView背景颜色始终显示为灰色

时间:2009-09-09 01:45:25

标签: android listview view themes

我有ListView我正在使用自定义ListAdapter填充。在适配器内部(在getView(int, View, ViewGroup)方法中)我使用View设置setBackgroundColor(int)的背景颜色。问题在于,无论我将背景设置为什么颜色,它总是出现深灰色。值得注意的是,我正在使用Light主题。

代码的相关(简化)位:

的AndroidManifest.xml:

<activity 
    android:name=".MyActivity"
    android:theme="@android:style/Theme.Light" />

MyAdapter.java:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View av = inflater.inflate(R.layout.my_row, parent, false);
    av.setBackgroundColor(R.color.myRow_red);
    mName = (TextView) av.findViewById(R.id.myRow_name);
    mName.setText("This is a name");
    return av;
}

有任何想法/建议吗?

6 个答案:

答案 0 :(得分:21)

你应该使用: setBackgroundResource(R.color.myRow_red) 而不是setBackgroundColor()在您的示例中,背景颜色分配了ID ,而不是资源中描述的实际颜色。

答案 1 :(得分:6)

您必须将cacheColorHint属性设置为列表所需的背景颜色。这是考虑Android在列表上执行的绘图优化所必需的解决方法。

见这里:link text

答案 2 :(得分:2)

我遇到了一个类似的问题,分隔线出现灰色。我发现其他解决方案没有效果,但我android:divider="<drawable,color, or whatever>" ListView工作了。

答案 3 :(得分:0)

冷却总是将整行包裹在另一个视图中,并在该视图上设置背景颜色。该视图将是该行的第一个(也是唯一的)子级。

答案 4 :(得分:0)

试试这个:

setBackgroundColor(0xFF5DB9FB);

答案 5 :(得分:0)

尝试这样做:

  

av.setBackgroundColor(getResources().getColor(R.color.myRow_red));

相关问题