state_activated and ListView item background color

你。 提交于 2019-12-24 00:07:08

问题


I'm trying to color an item's (when selected) background. I'm working with the Android emulator. I have a set of XML files in res/drawable.

background.xml :

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

normal.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#000000" />
</shape>

pressed.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#33ffff" />
</shape>

And my list_item.xml, with...my item : <TextView ... android:background="@drawable/background" android:padding="6dp" />

Well, in the emulator state_pressed="true" works, however I want that the selected item remains colored. So, instead of state_pressed I tried state_activated ...but hey...doesn't works too. My item remains black...

Need some help :) !

Thank you


回答1:


Pls refer to the link below:

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

try using state_focused for your case.

Thanks!




回答2:


I believe state_activated is what you are looking for.

In your listselector.xml :

<item android:state_activated="true" android:drawable="@drawable/whatever" />
<item android:state_activated="false" android:drawable="@android:color/transaprent" />

Then in your code, implement an onItemClickListener and set your view to activated:

view.setActivated(true);

Hope this helps.

Tom



来源:https://stackoverflow.com/questions/6422438/state-activated-and-listview-item-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!