【问题标题】:Android: ListView background color anomalyAndroid:ListView背景颜色异常
【发布时间】:2013-06-05 22:18:07
【问题描述】:

我在 ListView 上遇到问题,实际上行的背景颜色会随机更改,而无需由代码设置。我试图使 ListView 无效,更改可见性,删除所有子视图并重新创建它,但没有。

这是一个正确颜色的屏幕: right color

这是错误的: wrong color

行的颜色在getView方法中根据它们的位置设置,蓝色被赋予特定的行,设置该颜色的相同代码也会改变右边的图片。

我尝试调试代码,颜色分配没有错误..

分配背景颜色的一段代码:

View v = _vi.inflate(R.layout.row_prodotto_generale, parent, false);

LinearLayout la = (LinearLayout) v.findViewById(R.id.linearLayoutProdottoGenerale);
la.setBackgroundResource( ((position % 2) == 0) ? R.color.color_row_odd : R.color.color_row_even  );

final Prodotti c = (Prodotti)_data.get( position ); // Item in the row
if( c != null ) {
    // Set text label
    // Set text color to black
    // Set the plus image on the right

    if(Float.compare(c.getProdottoQta(), +0.0f) > 0) {
        // If the item has a specific field bigger than 0
        la.setBackgroundColor(getResources().getColor(R.color.toolbar_blue));
        // Set text color to white
        // Set the other image on the right
    }
}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutProdottoGenerale"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_gravity="right"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/rowProdottoGeneraleNome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:maxWidth="180dp"
            android:scrollHorizontally="true"
            android:text="@string/_nome_"
            android:textColor="@color/color_black"
            android:textSize="16sp"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/rowProdottoGeneraleMarca"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:maxWidth="180dp"
            android:scrollHorizontally="true"
            android:text="@string/_marca_"
            android:textColor="@color/color_dark_gray"
            android:textSize="14sp" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/addProdotto"
            android:background="@color/color_white" />

        <ImageView
            android:id="@id/addProdotto"
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/plus"
            android:src="@drawable/piu_blu" >
        </ImageView>
    </RelativeLayout>

</LinearLayout>

一般情况下,整行都是蓝色的背景色,出现问题时只有“linearLayoutProdottoGenerale”的第一个子元素的背景变成蓝色。

当我尝试搜索一个项目时,或者当我单击一行右侧的图像时,或者当我单击搜索框上方的顶视图时(顺便说一句是折叠视图),问题随机发生。 此外,问题仅出现在我的 Galaxy Tab 2 上,我还尝试了一些智能手机(Nexus S、Galaxy Ace)和模拟器,但这里一切正常。

【问题讨论】:

    标签: android listview colors background background-color


    【解决方案1】:

    添加else 子句:

    if (Float.compare(c.getProdottoQta(), +0.0f) > 0) {
        // If the item has a specific field bigger than 0
        la.setBackgroundColor(getResources().getColor(R.color.toolbar_blue));
        // Set text color to white
        // Set the other image on the right
    }
    else {
        la.setBackgroundResource( ((position % 2) == 0) ? R.color.color_row_odd : R.color.color_row_even  );
    }
    

    【讨论】:

    • 我试过了,如果行变成蓝色,文本标签也会改变,图像也会改变。在代码的其他部分我没有使用那种颜色
    • 您在 LinearLayout 上设置它对吗?似乎整个行都改变是有意义的。很难看到发生了什么。如果我是你,我会发布你的全部 getView()row_prodotto_generale.xml,以便人们可以提供更多帮助。
    • 添加了,希望找到解决办法
    • 我不确定这是否会导致问题,但您确实应该在getView() 中使用convertView。 (android-er.blogspot.co.uk/2010/06/…) 此外,您应该考虑使用ViewHolder (developer.android.com/training/improving-layouts/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多