我正在尝试设置一个包含具有不同背景颜色的项目的 ListView
,因此我的适配器的 getView()
方法调用 setBackgroundResource()
为所需的背景颜色提供适当的可绘制资源。
如果我使用引用 ColorStateList
作为其颜色的 ColorDrawable
,当我点击该项目。
如果我使用一个 StateListDrawable
为按下状态引用一个 ColorDrawable
并为未按下状态引用一个不同的 ColorDrawable
,我得到所需的当我点击该项目时突出显示效果。
我已经建立了一个简单的项目来演示这一点。这是 ListAdapter
中的 getView()
方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
TextView textView = (TextView)retval.findViewById(android.R.id.text1);
textView.setText("" + position);
switch ( position ) {
case 0:
retval.setBackgroundResource(R.drawable.list_background_item_0);
break;
case 1:
retval.setBackgroundResource(R.drawable.list_background_item_1);
break;
}
return retval;
}
res/drawable/list_background_item_0.xml:
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/list_background_item_0" />
res/color/list_background_item_0.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/list_background_item_0_pressed"/>
<item android:color="@color/list_background_item_0_default"/>
</selector>
res/drawable/list_background_item_1.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/list_background_item_1_pressed" />
<item android:drawable="@drawable/list_background_item_1_default" />
</selector>
res/drawable/list_background_item_1_default.xml:
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/list_background_item_1_default" />
res/drawable/list_background_item_1_pressed.xml:
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/list_background_item_1_pressed" />
res/values/colors.xml:
<resources>
<color name="list_background_item_0_default">#FFCCCC</color>
<color name="list_background_item_0_pressed">#996666</color>
<color name="list_background_item_1_default">#CCFFCC</color>
<color name="list_background_item_1_pressed">#669966</color>
</resources>
列表项 0 配置有引用 ColorStateList
的 ColorDrawable
,并且在按下时不显示突出显示。列表项 1 配置有 StateListDrawable
并显示突出显示。
与 StateListDrawable
相比,我更愿意使用 ColorStateList
,因为它看起来更干净并且每个项目类型涉及的文件更少。有没有我错过的东西可以让我使用这种方法?如果它没有像我预期的那样工作,有人可以解释原因吗?
最佳答案
感谢Luksprog的评论,我了解到 StateListDrawable
引用的可绘制对象可以是 colors.xml
中的颜色。这使得 StateListDrawable
解决方案优于 ColorStateList
版本。
最终的解决方案是...
getView()
方法:
- 没有变化
res/color/list_background_item_0.xml:
- 已删除
res/drawable/list_background_item_0.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/list_background_item_0_pressed" />
<item android:drawable="@color/list_background_item_0_default" />
</selector>
res/drawable/list_background_item_1.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/list_background_item_1_pressed" />
<item android:drawable="@color/list_background_item_1_default" />
</selector>
res/drawable/list_background_item_1_default.xml:
- 已删除
res/drawable/list_background_item_1_pressed.xml:
- 已删除
res/values/colors.xml:
- 没有变化
这个解决方案比我预期的 ColorStateList
版本更干净。
关于android - 为什么 ColorStateList 的按下状态不能与 ListView 项目一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21006631/