2011-02-16 72 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

相关问题