更改ListView项的背景颜色,但保持默认选择的风格? [英] Change ListView Item background color but keep the default selector style?

查看:309
本文介绍了更改ListView项的背景颜色,但保持默认选择的风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式设置自定义背景色在我的ListView适配器,但我想保持默认的列表视图选择风格从Android的。

我设置用我的项目视图中的SetBacgroundResource方法的背景颜色。

我使用的是Android 4.0,我试图从这个博客<一个例子href="http://daniel-$c$cs.blogspot.co.nz/2010/07/how-to-change-listview-rows-background.html">http://daniel-$c$cs.blogspot.co.nz/2010/07/how-to-change-listview-rows-background.html但是使用本例的选择器显示然而未选背景色未显示

我怎样才能在Android的4.0 ICS实现这一目标?

编辑:这是我使用列表项的背景绘制的资源

 &LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
   &LT;项目安卓state_window_focused =假的Andr​​oid版本:state_selected =真正的机器人:可绘制=@机器人:彩色/透明/&GT;
   &LT;项目安卓state_selected =真正的机器人:可绘制=@机器人:彩色/透明/&GT;
   &LT;项目的android:STATE_ pressed =真正的机器人:state_selected =假机器人:可绘制=@机器人:彩色/透明/&GT;
   &LT;项目安卓state_selected =假机器人:可绘制=@色/ list_item_first/&GT;
&LT; /选择器&GT;
 

这是我使用的设置此背景绘制的code是我adatapter的GetView方法中。在code是:

  convertView.setBackgroundResrouce(R.drawable.list_item_dark);
 

解决方案

你有没有尝试设置属性<一href="http://developer.android.com/reference/android/widget/AbsListView.html#attr_android%3adrawSelectorOnTop"相对=nofollow>安卓drawSelectorOnTop你的的ListView 来真正的?
您可以设置背景为您的项目,看项目点亮时,pressed。

I am trying to programmatically set a custom background color in my ListView Adapter but I want to keep the default Listview Selector style from Android.

I am setting the background color using the "SetBacgroundResource" method of my item view.

I am using Android 4.0 and I tried the example from this blog http://daniel-codes.blogspot.co.nz/2010/07/how-to-change-listview-rows-background.html but using this example the selector shows however the unselected background color is not showing.

How can I achieve this in Android 4.0 ICS?

EDIT: Here is the Resource I am using for the List Item background drawable.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_window_focused="false" android:state_selected="true" android:drawable="@android:color/transparent" /> 
   <item android:state_selected="true" android:drawable="@android:color/transparent" /> 
   <item android:state_pressed="true" android:state_selected="false" android:drawable="@android:color/transparent" /> 
   <item android:state_selected="false" android:drawable="@color/list_item_first" /> 
</selector>

The code that I am using to set this background drawable is inside the GetView method of my adatapter. The code is:

convertView.setBackgroundResrouce(R.drawable.list_item_dark);

解决方案

Did you try to set the property android:drawSelectorOnTop of your ListView to true ?
You will be able to set a background for your item and to see the item highlighted when pressed.

这篇关于更改ListView项的背景颜色,但保持默认选择的风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆