[英]android - programatically change listview background color
我創建了一個自定義列表視圖,並為其創建了一個適配器。 我希望能夠以編程方式在自定義列表視圖中更改不同視圖的不同樣式方面。 我的自定義列表視圖是其中包含TextViews的RelativeLayout。 我沒有運氣嘗試了以下內容。 沒有錯誤或任何東西。
View myView = adapter.getView(position, null, null);
myView.setBackgroundColor(Color.parseColor("#93E6CD"));
myView.findViewById(R.id.layout).setBackgroundColor(Color.parseColor("#93E6CD"));
adapter.notifyDataSetChanged();
這是我的自定義列表視圖
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:id="@+id/layout">
<!-- Title-->
<TextView
android:id="@+id/primaryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textColor="#040404"
android:typeface="sans"
android:textSize="18dip"
android:textStyle="bold"/>
<!-- Secondary title -->
<TextView
android:id="@+id/secondaryTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/primaryTitle"
android:textColor="#343434"
android:textSize="12dip"
android:layout_marginTop="1dip"
android:text="" />
<!-- Rightend Data -->
<TextView
android:id="@+id/rightData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/primaryTitle"
android:gravity="right"
android:text=""
android:layout_marginRight="5dip"
android:textSize="12dip"
android:textColor="#B53021"
android:textStyle="bold"/>
</RelativeLayout>
在您提供更多信息之前,我們將無法繼續。
我懷疑您沒有正確設置ListView。 您是否真的要顯示類似項目的列表,例如數據庫中的行? 如果是這樣,我希望以代碼或XML形式查看ListView對象的定義。 我也想看看適配器的定義。 看起來您已經為ListView定義了ListAdapter,但是我也想知道支持ListAdapter的內容。
線
myView.findViewById(R.id.layout).setBackgroundColor()
看起來可疑。 您似乎在尋找myView的子視圖,但是我看不到您在哪里定義了該子視圖。
通常,您將ListView定義為整個UI布局文件的一部分,然后將其定義為ListView中一個項目的單獨項目布局,然后將數據源調整為ListView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.