麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > 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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产美女视频一区 | 黄色毛片视频在线观看 | 久久久成人精品视频 | 毛片视频免费观看 | 99在线精品视频免费观看20 | 久久久久久久久免费 | 日本网站一区二区三区 | 欧美一级精品 | 黄色片网站免费 | 亚洲免费视频一区二区 | 在线成人www免费观看视频 | 国产精品久久在线观看 | 亚洲无限资源 | 国产精品片一区二区三区 | 99在线精品视频免费观看20 | 欧美一级性 | 久久精品a一级国产免视看成人 | 91在线视频导航 | 成人三级在线播放 | 欧美精品一区二区三区四区 | 深夜免费观看视频 | 欧美精品一区自拍a毛片在线视频 | 久久精品亚洲成在人线av网址 | 91精品国产91久久久久久吃药 | 双性精h调教灌尿打屁股的文案 | 久久久久久91 | 亚洲免费在线视频 | 国产91精品久久久 | 最近日本电影hd免费观看 | 黄色特级视频 | 激情综合婷婷久久 | 亚洲αv | 午夜伊人 | av在线免费不卡 | 免费国产在线观看 | 香蕉成人在线观看 | 91 免费视频| 激情影院在线观看 | 久久久久av69精品 | 999久久国精品免费观看网站 | 91精品片 |