Baby_Bonnie 2012-12-03 02:33 采纳率: 96.4%
浏览 3380
已采纳

改变选择列表项中的颜色

我想设置的功能是,当点击列表项时,列表项的颜色会改变。
我按以下的方法设计:
list_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    Selected 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/list_focused"/> 

  Pressed
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/list_selected" />  

</selector> 

在colors.xml 中设置颜色

 <drawable name="list_focused">#36C170</drawable>
  <drawable name="list_selected">#9EC136</drawable>

ListView

<ListView
            android:id="@+id/list_centers_complete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:cacheColorHint="@android:color/transparent"
            android:listSelector="@drawable/list_item_selector" />

但是当我点击列表项时,整个背景颜色都变了,而单个列表项的颜色没变。
怎么处理这个问题啊?

  • 写回答

3条回答 默认 最新

  • nana4ever 2012-12-03 09:07
    关注

    在列表视图的列中应用"@drawable/list_item_selector"本身就不是一个列表
    你的列表项类似于

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="@drawable/list_item_selector">
            <TextView       android:id="@+id/textForList"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:padding="10sp"  />
    .
    .
    .
    </LinearLayout>
    

    list_item_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_focused="true">
        <shape>
            <solid android:color="#66FFFFFF" />
        </shape>
    </item>
        <item>
        <shape>
            <solid android:color="#FF666666" />
        </shape>
    </item>
    
    </selector>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?