Android ListView 背景颜色总是显示灰色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1397183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:01:44  来源:igfitidea点击:

Android ListView background colors always showing grey

androidlistviewviewthemes

提问by Jeremy Logan

I have a ListViewthat I'm populating from a custom ListAdapter. Inside the Adapter (in the getView(int, View, ViewGroup)method) I'm setting the background color of the Viewusing setBackgroundColor(int). The problem is that no matter what color I set the background to it always comes out a dark grey. It might also be worth noting that I'm using the Light theme.

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

Relevant (simplified) bits of code:

相关(简化)代码位:

AndroidManifest.xml:

AndroidManifest.xml:

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

MyAdapter.java:

我的适配器.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;
}

Any ideas/suggestions?

任何想法/建议?

回答by Max Feldman

You should use: setBackgroundResource(R.color.myRow_red)instead of setBackgroundColor(). In your example background color is assigned with the IDinstead of the actual color described in the resources.

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

回答by Matthias

You must set the cacheColorHint attribute to the desired background color for your list. This is a required workaround to account for a drawing optimization Android performs on lists.

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

See here: link text

请参阅此处:链接文本

回答by Alex

I ran into a similar problem where the divider was coming up grey. I found that the other solutions had no effect, but android:divider="<drawable,color, or whatever>"on my ListViewworked.

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

回答by Vidar Vestnes

You cold always wrap the whole row inside another view and set the background color on that view. This view would be the first (and only) child of the row.

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

回答by Laces

try this:

尝试这个:

setBackgroundColor(0xFF5DB9FB);

回答by Rodrigo

Try to do like this:

尝试这样做:

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

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