2011-02-09 16 views
1

我有一個列表視圖,我希望背景在用戶單擊(觸摸)時改變顏色。ListView + Selector爲什麼會從xml文件崩潰

每個的listItem是休耕:

<?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> 

提拉我有休耕文件(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" android:state_pressed="true" android:background="@color/green" /> 
    <item android:state_pressed="true" android:background="@color/green" /> 
    <item android:background="@color/white" /> 
</selector> 

然後,我有值文件夾內的彩色文件( color.xml):

<?xml version="1.0" encoding="utf-8"?> 

<resources> 
    <color name="green">#808080</color> 
    <color name="white">#ffffff</color> 
</resources> 

當我嘗試運行顏色選擇器崩潰。如果我只用白色背景運行,不調用選擇器一切正常。

有人可以告訴我什麼是錯的?

謝謝, 阿德里安。

回答

1

代碼在XML的顏色不對......你需要的代碼前加「#FF」 ......

<?xml version="1.0" encoding="utf-8"?> 

    <resources> 
    <color name="green">#FF808080</color> 
    <color name="white">#FFFFFFFF</color> 
    </resources> 
+0

你能解釋爲什麼你認爲這是解決方案? – speedynomads 2013-07-30 13:07:32