简体   繁体   English

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

[英]Android ListView background colors always showing grey

I have a ListView that I'm populating from a custom ListAdapter . 我有一个ListView ,我从自定义ListAdapter填充 Inside the Adapter (in the getView(int, View, ViewGroup) method) I'm setting the background color of the View using setBackgroundColor(int) . 在适配器内部(在getView(int,View,ViewGroup)方法中)我使用setBackgroundColor(int)设置View的背景颜色。 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. 值得注意的是,我正在使用Light主题。

Relevant (simplified) bits of code: 相关(简化)代码:

AndroidManifest.xml: AndroidManifest.xml中:

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

MyAdapter.java: 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;
}

Any ideas/suggestions? 有什么想法/建议吗?

You should use: setBackgroundResource(R.color.myRow_red) instead of setBackgroundColor() . 您应该使用: setBackgroundResource(R.color.myRow_red)而不是setBackgroundColor() In your example background color is assigned with the ID instead of the actual color described in the resources. 在您的示例中,背景颜色分配了ID而不是资源中描述的实际颜色。

You must set the cacheColorHint attribute to the desired background color for your list. 您必须将cacheColorHint属性设置为列表所需的背景颜色。 This is a required workaround to account for a drawing optimization Android performs on lists. 这是考虑Android在列表上执行的绘图优化所必需的解决方法。

See here: link text 请参见此处: 链接文字

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 ListView worked. 我发现其他解决方案没有效果,但我的ListView工作的android:divider="<drawable,color, or whatever>"

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. 该视图将是该行的第一个(也是唯一的)子级。

试试这个:

setBackgroundColor(0xFF5DB9FB);

尝试这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM