how to change font color in selected/focused ListView items?

前端 未结 5 1460
心在旅途
心在旅途 2020-12-13 02:00

I am struggling with this which apparently is a very simple effect but incredibly haven\'t found any intutitive way for doing it in Android.

I have a ListView and I

相关标签:
5条回答
  • 2020-12-13 02:34

    Neither of these are possible answers when your ListView is compromised of a layout that has multiple views. You need to set your child views to:

    android:duplicateParentState="true"
    

    Now you can use the methods others have described above to declare your TextViews' colors using a selector such as:

    android:textColor="@drawable/my_row_selector"
    

    and I'm sure you're aware, but the selector can be as simple as:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="@color/white" />
        <item android:color="@color/black" />
    </selector>
    

    As you can see, @color values are allowed. Hope this helps.

    Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.

    0 讨论(0)
  • 2020-12-13 02:36

    In order to make it work on selection use the following code:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="#fff"/>
        <item android:state_activated="true" android:color="#fff"/>
        <item android:color="#000" />
    </selector>
    

    Apparently the key is state_activated="true" state.

    0 讨论(0)
  • 2020-12-13 02:39

    When you are deploying the app for Android 11+ (HoneyComb+), you should use

    android:state_activated="true"
    

    for selected list state. For the earlier versions use the combination of:

    android:state_checked="true"
    android:state_activated="true"
    

    Of course don't forget to include the

    android:duplicateParentState="true"
    

    so the view can get the activated/checked state from a parent list view item

    0 讨论(0)
  • 2020-12-13 02:41

    in your textview propeties

    android:textColor="@color/text_selector"
    

    in res/color text_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="YOUR_CUSTOM_COLOR" />
        <item android:state_selected="true" android:color="YOUR_CUSTOM_COLOR" />
        <item android:color="YOUR_CUSTOM_COLOR" />
    </selector>
    
    0 讨论(0)
  • 2020-12-13 02:46

    Also you may create a res/color folder and add a file "text_selector.xml" with the following lines:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_activated="true" android:color="#f0f"/>
        <item android:state_pressed="true" android:color="#f0f"/>
        <item android:color="#000"/>
    </selector>
    

    After that assign in TextView:

    android:textColor="@color/text_selector"
    
    0 讨论(0)
提交回复
热议问题