我有一个ListView
,和一个自定义样式,基本上看起来像霍洛,但黄色的口音,而不是蓝色。为ListView自定义overscroll发光/边缘颜色?
当我滚动到列表的底部或顶部时,我得到不合身的蓝色。我为overscroll_glow.png
和overscroll_edge.png
制作了自定义绘图,但我不知道如何使用它们。
我怎样才能让我的ListView
使用这些可绘制而不是系统的?
OverScroller
这个班会有帮助吗?
我有一个ListView
,和一个自定义样式,基本上看起来像霍洛,但黄色的口音,而不是蓝色。为ListView自定义overscroll发光/边缘颜色?
当我滚动到列表的底部或顶部时,我得到不合身的蓝色。我为overscroll_glow.png
和overscroll_edge.png
制作了自定义绘图,但我不知道如何使用它们。
我怎样才能让我的ListView
使用这些可绘制而不是系统的?
OverScroller
这个班会有帮助吗?
试过吗? setOverscrollFooter(Drawable)?
更新1:
哎呀。没关系。您可能已经看过this discussion。
发光行为包裹在EdgeEffect class中。您可以通过在您的ListView上调用setOverScrollMode(OVER_SCROLL_NEVER)
来禁用默认的边缘效果,然后滚动您自己的EdgeEffect类(其细节在此处超出我的范围)。
更新2:
哎哟。在AbsListView中创建的EdgeEffect对象上的调用深藏在滚动逻辑中。可能会很艰难。一个简单的解决方法是向AbsListView中的setEdgeEffect(EdgeEffect)
方法发送一个针对Android团队的功能请求...
只需使用此库。我只有在没有过度滚动(HTC One X/Sense 4.5)的设备上在横向上的弹出式菜单上环绕上下文时出现问题。奇迹般有效!
可以使用反射式的解决方案,以自己的方式破解改变这种发光颜色:
int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);
我遇到同样的问题,这似乎是一个很好的解决方案,至少直到Google添加了一个用于更改颜色(或品牌颜色)的API:http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/
乘法不好,因为它会将你的颜色与默认的蓝色混合在一起,你应该使用SrcAtop,它将完全替换原来的颜色 –
对于ScrollView而言,这个代码在哪里调用? – chochim
Hey Menny感谢您的帖子我似乎无法得到这个工作我有上下文设置像这样的'Context context = this;' – iqueqiorio
我添加了此答案,因为这是该主题的顶级结果之一,并且顶级答案不再正确。 那么,它实际上是棒棒糖之前的版本,但如预期,当您使用Java反射你得到咬伤回来了,这是发生了什么,如果你使用的代码
int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(context.getResources().getColor(R.color.gm_color), PorterDuff.Mode.MULTIPLY);
这将在棒棒堂+设备崩溃,因为该资源不再存在。所以对于android 5。+你应该使用:
<item name="android:colorEdgeEffect">@color/custom_color</item>
在你的主题,在values-v21/themes.xml
文件重写,如果你不分钟21版
这里针对你的解决方案:
public static void ChangeEdgeEffect(Context cxt, View list, int color){
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
EdgeEffect edgeEffectTop = new EdgeEffect(cxt);
edgeEffectTop.setColor(color);
EdgeEffect edgeEffectBottom = new EdgeEffect(cxt);
edgeEffectBottom.setColor(color);
try {
Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
f1.setAccessible(true);
f1.set(list, edgeEffectTop);
Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
f2.setAccessible(true);
f2.set(list, edgeEffectBottom);
} catch (Exception e) {
e.printStackTrace();
}
}else{
int glowDrawableId = cxt.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = cxt.getResources().getDrawable(glowDrawableId);
assert androidGlow != null;
androidGlow.setColorFilter(cxt.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP);
}
}
此方法运行良好,谢谢。 – BArtWell
请不要忘记改变overscroll边缘线:
final int edgeDrawableId = res.getIdentifier("overscroll_edge", "drawable", "android");
final Drawable overscrollEdge = res.getDrawable(edgeDrawableId);
overscrollEdge.setColorFilter(res.getColor(colorID), android.graphics.PorterDuff.Mode.SRC_ATOP);
试过了页脚,不是我所需要的。 所以我做这件事的唯一方法是创建一个自定义的'AbsListView'类?太喜欢我喜欢:( –
嗯,我现在确信,它可以通过继承ListView和重写一堆方法('onOverScrolled()'是重要的)),但我不知道它是否值得它:( – heycosmo