android布局背景顏色,android listview item 選中背景,Android ListView的item背景色設置和item點擊無響應的解決

 2023-12-12 阅读 26 评论 0

摘要:下面講解以下在使用listview時最常見的幾個問題。1.如何改變item的背景色和按下顏色listview默認情況下,item的背景色是黑色,在用戶點擊時是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:1)設置listSelector2)在布局文件中設置i

下面講解以下在使用listview時最常見的幾個問題。

1.如何改變item的背景色和按下顏色

listview默認情況下,item的背景色是黑色,在用戶點擊時是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:

1)設置listSelector

2)在布局文件中設置item的background

3)在adapter的getview中設置

這三種方法都能達到改變item默認的背景色和按下顏色,下面來分別講解,但是在這之前需要先寫好selector.xml文件;

xmlns:android="http://schemas.android.com/apk/res/android">

在改變button或者listview的item默認背景色,就可以用到selector。drawable可以設置為色彩資源,也可以設置為圖片資源。

1)設置listview的listSelector

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">

2)在listitem的布局文件中設置background屬性,下面是listitem的布局文件

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/selector">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="歷史記錄"

android:textColor="#ffffff"

android:textSize="20sp"

android:layout_centerInParent="true">

3)在adapter的getView方法中設置

if(convertView ==null)

{

convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);

}

convertView.setBackgroundResource(R.drawable.selector);

上述方法都能達到同樣的效果,就是改變item默認的背景色和點擊時的背景顏色,第三種方法最靈活,如果listview的奇數行和偶數行需要設置為不同的selector,只能用第三種方法。

2.包含button,checkbox等控件時點擊無響應問題。

如果listitem里面包括button或者checkbox等控件,默認情況下listitem會失去焦點,導致無法響應item的事件,最常用的解決辦法是在listitem的布局文件中設置descendantFocusability屬性。

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">

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"

>

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">

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">

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">

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">

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/194445.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息