state_activated和ListView项目背景颜色

时间:2011-06-21 08:32:06

标签: android android-layout android-emulator

我正在尝试为项目(如果选择)背景着色。我正在使用Android模拟器。我在res/drawable

中有一组XML文件

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>

我的list_item.xml,带有...我的项目: <TextView ... android:background="@drawable/background" android:padding="6dp" />

好吧,在模拟器state_pressed="true"中,我希望所选项目保持彩色。所以,而不是state_pressed我尝试了state_activated ......但是嘿......也不行。我的物品仍然是黑色......

需要一些帮助:)!

谢谢

2 个答案:

答案 0 :(得分:5)

我相信state_activated正是你要找的。

在listselector.xml中:

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

然后在您的代码中,实现onItemClickListener并将您的视图设置为激活:

view.setActivated(true);

希望这有帮助。

汤姆

答案 1 :(得分:0)

请参阅以下链接:

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

尝试使用state_focused为您的情况。

谢谢!