我有一个列表视图,当用户点击该项时,它看起来像这样。
这就是它看起来像是noramally:
如何更改此默认浅蓝色?
我需要在活动文件中以编程方式设置颜色,因为它并不总是相同的颜色,还有如何在继续使用时更改ListView底部出现的模糊内容的颜色向下滚动?
感谢您的帮助
修改
另外,如何更改操作栏后退按钮的按下颜色,
答案 0 :(得分:3)
对于行颜色,您需要创建StateListDrawable
和set it as the selector for the ListView
。
要包括的州至少应该是:
android.R.attr.state_pressed
)android.R.attr.state_selected
)对于每个州,您可以设置任何可绘制的。如果您需要的是纯色,则可以在现场以编程方式创建ColorDrawable
。
例如:
StateListDrawable selector = new StateListDrawable();
ColorDrawable red = new ColorDrawable(Color.RED);
ColorDrawable transparent = new ColorDrawable(Color.TRANSPARENT);
selector.addState(new int[] { android.R.attr.state_pressed }, red);
selector.addState(new int[] { android.R.attr.state_selected }, red);
selector.addState(new int[] { }, transparent);
listView.setSelector(selector);
至于“底部出现的模糊东西”,它稍微复杂一些。
对于5.0之前的版本,有一个众所周知的解决方法(described here),它涉及篡改所使用的drawable。但是,此解决方案在5.0中崩溃,因为这些资源不再存在。
对于Android 5.0,如果可以接受静态颜色,则可以configure the new theme attribute android:colorEdgeEffect
(默认情况下与android:colorPrimary
相同)。
如果需要Android 5.0的编程解决方案,您也可以使用反射更改ListView内部的EdgeEffect
个对象。我已经测试了它并且它可以工作,虽然它不是最漂亮的代码:
EdgeEffect edgeEffectTop = new EdgeEffect(this);
edgeEffectTop.setColor(Color.RED);
EdgeEffect edgeEffectBottom = new EdgeEffect(this);
edgeEffectBottom.setColor(Color.RED);
try {
Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
f1.setAccessible(true);
f1.set(listView, edgeEffectTop);
Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
f2.setAccessible(true);
f2.set(listView, edgeEffectBottom);
} catch (Exception e) {
e.printStackTrace();
}
因此,完整的程序化解决方案可能会运行如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
// For Android >= 5.0, use setColor() on EdgeEffect
EdgeEffect edgeEffectTop = new EdgeEffect(this);
edgeEffectTop.setColor(Color.RED);
EdgeEffect edgeEffectBottom = new EdgeEffect(this);
edgeEffectBottom.setColor(Color.RED);
try {
Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
f1.setAccessible(true);
f1.set(listView, edgeEffectTop);
Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
f2.setAccessible(true);
f2.set(listView, edgeEffectBottom);
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
// For Android < 5.0, change overscroll_glow and overscroll_edge
int glowDrawableId = getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
int edgeDrawableId = getResources().getIdentifier("overscroll_edge", "drawable", "android");
Drawable androidEdge = getResources().getDrawable(edgeDrawableId);
androidEdge.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
}
答案 1 :(得分:0)
如何更改此默认浅蓝色?
您必须创建StateListDrawable
并将其设置为selector
的{{1}}。 matiash的答案是好的,我不想再解释它。
更改a底部出现的模糊内容的颜色 继续向下滚动时ListView
另外,如何更改操作栏的轻敲颜色 按钮
ListView
然后在actionbar_item_background中:
<style name="actionbarCustomBackground" parent="@style/Theme.Holo.Light" >
<item name="android:selectableItemBackground">@drawable/actionbar_item_background</item>
</style>
答案 2 :(得分:-2)
您可以在getView(..)
方法中动态设置行的背景。
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
.......... //your code
view.setBackgroundColor(Color.RED)
return view;
}