a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android ListView的item背景色設置和item點擊無響應的解決方法

2020-04-11 12:01:14
字體:
來源:轉載
供稿:網友

  下面講解以下在使用listview時最常見的幾個問題。
1.如何改變item的背景色和按下顏色
  listview默認情況下,item的背景色是黑色,在用戶點擊時是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:
  1)設置listSelector
  2)在布局文件中設置item的background
  3)在adapter的getview中設置
  這三種方法都能達到改變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>

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

2)在listitem的布局文件中設置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方法中設置
復制代碼 代碼如下:

 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屬性。
復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美激情小视频 | 特黄视频| 在线国产一区二区 | 操久久| 久久久久久久久久久久久久av | 精品久久久久国产免费 | 免费黄色片视频网站 | 黄a网 | 国产一区二区免费 | 精品一区av| 激情毛片| 福利一区二区 | 97人人草 | 伊人天堂网| 国产ts余喵喵和直男多体位 | 在线观看国产精品一区 | 色com | 在线h观看 | 91偷拍精品一区二区三区 | 九九久久精品 | 一级黄色在线 | 日韩久久久一区二区 | 超碰人人射 | 亚洲国产精品久久久久秋霞不卡 | 欧美日韩中文在线 | 亚洲人人 | 午夜欧美 | 男女网站在线观看 | 99精品欧美一区二区三区综合在线 | 欧美伦理一区二区 | 欧美一区二区三区在线播放 | 欧美一区 | 久久99久久久久 | 男人的天堂久久 | 日本精品一区二区三区在线观看视频 | 国产在线小视频 | 国产精品色网站 | 爱爱视频免费 | 国产精品一区av | 中文字幕天堂在线 | 免费在线色|