繁体   English   中英

Android:ListView背景色异常

[英]Android: ListView background color anomaly

我在ListView上遇到问题,实际上行的背景颜色在没有代码设置的情况下随机更改。.我试图使ListView无效,更改可见性,删除所有子项并重新创建它,但是什么也没有。

这是正确颜色的屏幕: 正确颜色

这是一个错误的错误的颜色

在方法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)和仿真器,但是在这里一切正常。

添加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  );
}

暂无
暂无

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

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