Android ListView的item背景色設(shè)置和item點(diǎn)擊無(wú)響應(yīng)的解決方法
下面講解以下在使用listview時(shí)最常見的幾個(gè)問題。
1.如何改變item的背景色和按下顏色
listview默認(rèn)情況下,item的背景色是黑色,在用戶點(diǎn)擊時(shí)是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:
1)設(shè)置listSelector
2)在布局文件中設(shè)置item的background
3)在adapter的getview中設(shè)置
這三種方法都能達(dá)到改變item默認(rèn)的背景色和按下顏色,下面來(lái)分別講解,但是在這之前需要先寫好selector.xml文件;
<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>
在改變button或者listview的item默認(rèn)背景色,就可以用到selector。drawable可以設(shè)置為色彩資源,也可以設(shè)置為圖片資源。
1)設(shè)置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>
2)在listitem的布局文件中設(shè)置background屬性,下面是listitem的布局文件
<?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>
3)在adapter的getView方法中設(shè)置
if(convertView ==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
}
convertView.setBackgroundResource(R.drawable.selector);
上述方法都能達(dá)到同樣的效果,就是改變item默認(rèn)的背景色和點(diǎn)擊時(shí)的背景顏色,第三種方法最靈活,如果listview的奇數(shù)行和偶數(shù)行需要設(shè)置為不同的selector,只能用第三種方法。
2.包含button,checkbox等控件時(shí)點(diǎn)擊無(wú)響應(yīng)問題。
如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會(huì)失去焦點(diǎn),導(dǎo)致無(wú)法響應(yīng)item的事件,最常用的解決辦法是在listitem的布局文件中設(shè)置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>
- android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼
- Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法
- Android編程實(shí)現(xiàn)ListView中item部分區(qū)域添加點(diǎn)擊事件功能
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊item改變顏色功能示例
- Android 實(shí)現(xiàn)ListView的點(diǎn)擊變色的實(shí)例
- Android ListView的Item點(diǎn)擊效果的定制
- Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
相關(guān)文章
android實(shí)現(xiàn)自動(dòng)滾動(dòng)的Gallary控件效果
這篇文章主要介紹了android實(shí)現(xiàn)自動(dòng)滾動(dòng)的Gallary控件效果,涉及Android中Gallary控件的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10go語(yǔ)言之美迅速打rpm包實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了go語(yǔ)言之美迅速打rpm包實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android?使用maven?publish插件發(fā)布產(chǎn)物(aar)流程實(shí)踐
這篇文章主要介紹了Android?使用maven?publish插件發(fā)布產(chǎn)物(aar)流程實(shí)踐,Android?Gradle插件根據(jù)項(xiàng)目gradle中應(yīng)用不同的插件類型在編譯組裝后會(huì)生成不同的產(chǎn)物,具體相關(guān)介紹,需要的小伙伴可以參考一下2022-09-09深入android Unable to resolve target ''android-XX''詳解
本篇文章是對(duì)android Unable to resolve target 'android-XX'錯(cuò)誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android限時(shí)搶購(gòu)倒計(jì)時(shí)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android限時(shí)搶購(gòu)倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02android中colors.xml顏色設(shè)置資源文件的方法
這篇文章主要介紹了android中colors.xml顏色設(shè)置資源文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識(shí)別
這篇文章主要介紹了android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能,帶多音字識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02