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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable