Android ListView的item背景色設定與item點選無回應的解決方法
以下講解以下在使用listview時最常見的幾個問題。
1.如何改變item的背景色和按下顏色
listview預設情況下,item的背景色是黑色,在使用者點擊時是黃色的。若需要修改為自訂的背景顏色,一般情況下有三種方法:
1)設定listSelector
2)在版面配置檔案中設定item的background
3)在adapter的getview中設定為item 這三種方法都能達到改變item預設的背景色和按下顏色,下面來分別講解,但是在這之前需要先寫好selector.xml檔;
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/light_blue"></item> <item android:state_pressed="false" android:drawable="@color/sgray"></item> </selector>
1)設定listview的listSelector
<ListView android:id="@+id/history_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#565C5D" android:dividerHeight="3dp" android:listSelector="@drawable/selector" android:cacheColorHint="@android:color/transparent"> </ListView>
<?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="fill_parent" android:background="@drawable/selector"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="历史记录" android:textColor="#ffffff" android:textSize="20sp" android:layout_centerInParent="true"> </TextView> </RelativeLayout>
if(convertView ==null) { convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null); } convertView.setBackgroundResource(R.drawable.selector);
2.包含button,checkbox等控制項時點選無回應問題。
如果listitem裡麵包括button或者checkbox等控件,默認情況下listitem會失去焦點,導致無法響應item的事件,最常用的解決辦法是在listitem的佈局文件中設置descendantFocusability屬性。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:descendantFocusability="blocksDescendants"> <CheckBox android:id="@+id/history_item_checkbt" android:layout_height="30dp" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:checked="false" > </CheckBox> <ImageView android:id="@+id/history_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/history_item_checkbt" android:background="@drawable/item_icon"> </ImageView> <Button android:id="@+id/history_item_edit_bt" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="编辑" android:textColor="#ffffff" android:textSize="14sp" android:background="@drawable/button_bg"> </Button> <TextView android:id="@+id/history_item_time_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="10-01 10:20" android:layout_marginRight="5dp" android:layout_toLeftOf="@id/history_item_edit_bt"> </TextView> <TextView android:id="@+id/history_item_title_tv" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="xxxxxxxxXXXXXXXXXXXXXXXX" android:ellipsize="end" android:maxLines="1" android:layout_toRightOf="@id/history_item_image" android:layout_toLeftOf="@id/history_item_time_tv" android:layout_marginLeft="3dp"> </TextView> </RelativeLayout>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...
