2012-06-30 251 views
4

嗨我使用的ListView具有多個項目動態添加.. 我想改變列表項的文字顏色的任何想法更改ListView項項的文字顏色

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#659EC7"> 

    <ImageView android:id="@+id/icon" android:layout_width="72px" 
     android:layout_height="wrap_content" 
       android:layout_marginTop="5px" /> 

    <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
     android:orientation="vertical" 
       android:paddingTop="5px" 
       android:paddingBottom="5px"> 

     <TextView android:id="@+id/item1" android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView android:id="@+id/item2" android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceSmall" 
      android:paddingTop="30px" /> 
      <View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="@android:color/darker_gray"/> 

    </TwoLineListItem> 

</LinearLayout> 

我使用2linelistIem

+2

看到這篇文章http://stackoverflow.com/questions/4247385/how-to-change-color-of-listview-items-on-focus-and-on-click –

回答

1

這裏是我寫的代碼,並用於: 它使第所有其他州的Android風格。它只是覆蓋了透明的狀態,您想要的顏色:

Layer list drawable for list view with custom background

,你可以看到在我的問題有一個小錯誤與聚焦狀態,但它仍然是我見過的最好的解決辦法:d

1

使用此技術的類型動態地改變顏色:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused --> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed--> 
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed --> 
    <item android:drawable="@color/black" /> <!-- default --> 
</selector>