Android的 - 我设置一个ListView自定义背景以及高亮已经 [英] Android - I set a custom background for a listView, and the highlight has gone
问题描述
好了,现在这是我的SimpleAdapter的getView功能:
Ok, now this is my SimpleAdapter's getView function:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setBackgroundColor(R.drawable.color1);
return view;
}
}
这是我color1.xml文件,在res /绘-LPI文件夹:
And this is my color1.xml file, in res/drawable-lpi folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFF00FF"/> <!-- pressed -->
<item android:state_selected="true"
android:color="#FF0000FF"/> <!-- selected -->
<item android:state_focused="true"
android:color="#FF0000FF"/> <!-- focused -->
<item android:color="#FFFFFFFF"/> <!-- default -->
</selector>
为什么我仍然得到呢?
Why i still get this?
推荐答案
首先,你需要知道一个视图的所有后台设置功能,真正改变它的背景抽拉。
First of all, you need to know that all background settings functions of a View, actually change its background-drawable.
- setBackgroundColor实际上
创建一个ColorDrawable。 - setBackgroundResource 实际加载绘制资源
- 的<一个href=\"http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable%28android.graphics.drawable.Drawable%29\"相对=nofollow> setBackgroundDrawable ,很明显,采用了绘制。
- The setBackgroundColor actually creates a ColorDrawable.
- The setBackgroundResource actually loads a drawable resource
- The setBackgroundDrawable, obviously, uses a drawable.
当你调用setBackgroundColor你highlight.xml,你真正尝试创建一个ColorDrawable与R.drawable.highlight的生成的ID的值。
When you call setBackgroundColor with your highlight.xml, you actually try to create a ColorDrawable with the value of the generated ID of R.drawable.highlight.
您确实需要调用setBackgroundResource方法,它等同于机器人:背景。XML标签
You actually need to call the setBackgroundResource method, which is equivalent to the "android:background" XML tag.
您highlight.xml实际上是一个国家列表,其中包含的 StateListDrawable 。
如果你想只改变颜色,你可以使用 color-国家列表。
Your highlight.xml is actually a state-list, which is an instance of StateListDrawable. If you wanted to only change the colors, you could use a color-state-list.
这篇关于Android的 - 我设置一个ListView自定义背景以及高亮已经的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!