2011-02-16 73 views
0

我已經創建了一個自定義列表並設法更改正常和單擊視圖中子項目的顏色。但是,當我將它應用到焦點視圖時,則不會發生任何反應,並且只有文本的顏色從白色變爲黑色纔會顯示默認背景顏色。這裏是我用於迄今爲止所做的xml文件。改變焦點上的自定義列表視圖項目的顏色

佈局列表視圖(listview.xml)

<?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:orientation="horizontal" android:padding="18sp" 
    android:background="@drawable/backgroundselector"> 
    <ImageView android:id="@+id/imageView1" 
     android:layout_height="fill_parent" android:layout_gravity="center_vertical" 
     android:layout_width="wrap_content" /> 
    <TextView android:text="TextView" android:id="@+id/textView1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:paddingLeft="10sp" android:textSize="20sp" /> 
</LinearLayout> 

clicked.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
    <solid android:color="#ffc61212" /> 
</shape> 

focussed.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
    <solid android:color="#ff26e0d7" /> 
</shape> 

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="#ff0a4d66" /> 
</shape> 

最後backgroundselector.xml

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@drawable/focussed" /> <!-- focused --> 
    <item android:state_pressed="true" android:drawable="@drawable/clicked" /> <!-- pressed --> 
    <item android:drawable="@drawable/normal" /> <!-- default --> 
</selector> 

請我需要我如何可以改變一個小孩項目集中我的列表視圖的子項的顏色幫助...

回答

0

我的建議是覆蓋ListView.onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)方法,這將在單擊項目時調用。

而在此方法中,您可以創建一個新的ArrayAdapter。像這樣: ArrayAdapter<String> adpater=new ArrayAdapter<String>(CurrentActivity.this,R.layout.foucs,data);其中data是用於爲ListView提供數據的對象數組(通常,我們使用String []或ArrayList類)。

然後,撥打ListView.setAdapter(adapter)來展示您的新風格。

希望它有幫助。我是貪心:)

啊,我找到一個新的和方便的方法來做到這一點。

ListView.onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)方法中, 只需撥打arg1.setBackgroundColor(newColor);即可更改項目的顏色。

+0

但是,這是爲了當點擊的ListView的子節點。我期待的東西,當孩子的項目是集中,它的顏色會改變......無論如何感謝您的幫助。 – Piyush 2011-02-16 15:03:51

相關問題