点击时如何更改列表视图的颜色?

时间:2011-07-27 06:05:18

标签: android android-layout

Android默认情况下,在点按以显示点按状态时会显示绿色。我想将该颜色更改为其他颜色,以便在列表视图中的故事被点击时显示更改的颜色。

感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

只需在ListView中定义 android:listSelector 属性。

 <ListView 
        android:id="@+id/ListView" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@android:color/darker_gray">
     </ListView>

更新

根据chirag的评论,我想建议此链接作为参考:http://developer.android.com/reference/android/R.attr.html#listSelector

只需检查上面链接中的第一行:“Drawable用于指示列表中当前选定的项目。”

更新2:

只需在Drawable文件夹中定义ListSelector xml文件,然后输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/bg_list_item_selected"/>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bg_list_item_selected"/>
    <item android:state_focused="true" android:drawable="@drawable/bg_list_item_selected"/>
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/bg_list_item_normal"/>
</selector>

并将 android:background =“@ drawable / listSelctor”提供给自定义列表视图行文件。是的,它是自定义列表视图的自定义列表视图行文件。

答案 1 :(得分:0)

通过咨询老年人和同事,我得到了更好的答案,其中说:

  1. 制作与所需颜色列表视图的一个单元格相同的高度和宽度的图像。
  2. 将该图像放在路径/ res / drawable
  3. 下项目的drawable文件夹中
  4. 转到xml文件,其中设置了listview的属性。
  5. 添加一行listselector和其他属性,并将列表选择器的路径设置为最近放置在drawable文件夹中的图像。

    <ListView 
    android:id="@+id/ListView" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector = "@drawable/list_selector">
    </ListView>
    
  6. 清理/构建项目并运行。

  7. 我希望这是执行此任务最简单的方法......这对我很有用......希望这也适合你。