开发者

state_activated and ListView item background color

开发者 https://www.devze.com 2023-03-14 13:25 出处:网络
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.

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


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


Pls refer to the link below:

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

try using state_focused for your case.

Thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消