簡體   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