【发布时间】:2014-12-08 22:10:37
【问题描述】:
我有一个列表视图,当用户点击项目时,它看起来像这样。
正常情况下是这样的:
如何更改此默认浅蓝色?
我需要在活动文件中以编程方式设置颜色,因为它并不总是相同的颜色,以及当您继续时如何更改 ListView 底部出现的模糊内容的颜色向下滚动?
感谢您的帮助
编辑
还有如何改变操作栏后退按钮的点击颜色,
【问题讨论】:
我有一个列表视图,当用户点击项目时,它看起来像这样。
正常情况下是这样的:
如何更改此默认浅蓝色?
我需要在活动文件中以编程方式设置颜色,因为它并不总是相同的颜色,以及当您继续时如何更改 ListView 底部出现的模糊内容的颜色向下滚动?
感谢您的帮助
编辑
还有如何改变操作栏后退按钮的点击颜色,
【问题讨论】:
对于行颜色,您需要创建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),其中涉及篡改所使用的可绘制对象。然而,该解决方案在 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);
}
【讨论】:
ListView 实例的地方(可能是onCreate() 或onCreateView(),如果它是一个片段)。
onCreate() 中,但我看不出有什么区别,是不是我遗漏了什么?我在逻辑 Could not find method android.widget.EdgeEffect.setColor, referenced from method 中看到了这一点
targetSdkVersion 21 和 compileSdkVersion 21 中有这个设置
如何更改此默认浅蓝色?
您必须创建StateListDrawable 并将其设置为selector 的ListView。 matiash的答案很好,我不想再解释了。
更改出现在底部的模糊内容的颜色 继续向下滚动时的 ListView
另外你如何改变操作栏的点击颜色 按钮
<style name="actionbarCustomBackground" parent="@style/Theme.Holo.Light" >
<item name="android:selectableItemBackground">@drawable/actionbar_item_background</item>
</style>
然后在actionbar_item_background中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@android:color/holo_orange_dark" />
<item
android:state_pressed="true"
android:drawable="@android:color/holo_orange_dark" />
<item
android:drawable="@android:color/transparent" />
</selector>
【讨论】:
setSelectableItemBackground
您可以在getView(..) 方法中动态设置行的背景。
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
.......... //your code
view.setBackgroundColor(Color.RED)
return view;
}
【讨论】: