繁体   English   中英

选中列表视图时更改列表项的背景色

[英]Change background color of list item in listview when it is selected

选择列表视图时,如何更改列表项的背景颜色。

在res / drawable文件夹中,我创建了listselector.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/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 
</selector>

然后在values / colors.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <drawable name="focused">#ff5500</drawable>
    <drawable name="selected">#FF00FF</drawable>

</resources>

这是listview.xml

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
           android:layout_below="@+id/textView6"
        android:layout_height="75dp"
          android:textColor="#FFFFFF"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="33dp"
         >
    </ListView>

</LinearLayout>

但是在选择时不会更改列表项的背景颜色...

ListView和选择器之间没有链接。 您需要将以下代码行添加到listview.xml

android:listSelector=“@drawable/listselector"

我希望这有帮助。

这里 尽管较旧,但在单击项目时实现自定义颜色。 我认为它必须与android:drawable做一些事情。 尝试使用

android:color="@color/yourcolor"

并将可绘制对象设置为颜色:

<color name="focused">#ff5500</color>
<color name="selected">#FF00FF</color>

它在/ res / values / colors文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM