简体   繁体   English

如何为ListView设置选择器?

[英]How can I set selector for my ListView?

I have following problem: my application has layout with white background and ListView. 我有以下问题:我的应用程序具有白色背景和ListView的布局。 If I need to scroll this ListView then it changes color for black! 如果我需要滚动这个ListView,那么它会改变黑色的颜色! I have just made selector the for ListView, but it doesn't work while. 我刚为ListView制作了选择器,但它不起作用。 My ListView: 我的ListView:

        <ListView
            android:id="@+id/listViewLastUpdates"
            android:listSelector="@drawable/selector"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

My selector: 我的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/green" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/green" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/white" /> <!-- default -->
</selector> 

I need to user green color as highlight and white color as background (then if I scroll ListView it won't change a color). 我需要使用绿色作为高亮颜色和白色作为背景(然后如果我滚动ListView它将不会改变颜色)。 Please, tell me, how should I fix it? 请告诉我,我该如何解决?

Try this and tell me if it helped: 试试这个并告诉我它是否有帮助:

<ListView
    android:id="@+id/listViewLastUpdates"
    android:listSelector="@drawable/selector"
    android:background="@android:color/white"
    android:cacheColorHint="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

use color selector instead of drawable selector as you are setting color. 在设置颜色时使用color selector而不是drawable selector

Make color folder inside res and save this color xml selector and use it: color folder inside res并保存此颜色xml选择器并使用它:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/green" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:color="@color/green" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:color="@color/green" /> <!-- pressed -->
    <item android:color="@color/white" /> <!-- default -->
</selector> 

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

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