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

Jer*_*gan 9 android listview themes view

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

相关(简化)代码:

AndroidManifest.xml中:

<activity 
    android:name=".MyActivity"
    android:theme="@android:style/Theme.Light" />
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

有什么想法/建议吗?

小智 21

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


Mat*_*ias 6

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

请参见此处:链接文字