I have a list view, that I want the background to change color when it clicked (Touched) by the user.
Each listItem is as fallows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/row_background">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/RowImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight = "1"
android:background="#ffffff"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:minHeight="48dp"
android:maxHeight="48dp" />
<TextView android:id="@+id/RowText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="16dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000080"
android:background="#ffffff"
android:layout_weight = "2" />
</LinearLayout>
</LinearLayout>
Inside the drawable I have the fallowing file (row_background.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:background="@color/white" />
<item android:state_focused="true" andro开发者_开发知识库id:state_pressed="true" android:background="@color/green" />
<item android:state_pressed="true" android:background="@color/green" />
<item android:background="@color/white" />
</selector>
And then I have the color file inside the values folder (color.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="green">#808080</color>
<color name="white">#ffffff</color>
</resources>
When I try to run with the colors the selector it crashes. If I run with only white background, without calling the selector everything works ok.
Can somebody tell me what is wrong?
Thanks, Adrian.
Codes for the colors in the XML are wrong... you need to add '#FF' before the code...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="green">#FF808080</color>
<color name="white">#FFFFFFFF</color>
</resources>
精彩评论