1

在 结束时HorizontalScrollView,会出现一个灯,表示滚动内容已结束。有没有办法改变这种颜色?它在我的手机中显示为黄色。我已经将HorizontalScrollView的背景颜色设置为我想要的颜色,但是这个“滚动结束”灯不是我想要的。

编辑:我刚刚注意到这个灯的出现是由于onOverScrollMode(从 API 级别 9 开始 - 请参阅此链接)。有没有办法设置 OVER_SCROLL_NEVER 并保持与 Eclair 版本的兼容性?甚至更好:设置此灯的颜色(如果出现)?

4

3 回答 3

2

不幸的是,没有简单的方法来设置 OverScroll EdgeEffect 的颜色。

为了安全地设置 OVER_SCROLL_NEVER 并保持与早期 SDK 版本的兼容,您可以自省 setOverScrollMode 方法,如果发现调用它。(在 3.1 和 2.2 上测试)

    setContentView(R.layout.main);

    // find scroll view         
    HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.hscroll);
    try {
        // look for setOverScrollMode method
        Method setOverScroll = hscroll.getClass().getMethod("setOverScrollMode", new Class[] { Integer.TYPE } );

        if (setOverScroll != null) {
            try {
              // if found call it (OVER_SCROLL_NEVER == 2)
              setOverScroll.invoke(hscroll, 2);
            } catch (InvocationTargetException ite) {       
            } catch (IllegalAccessException ie) {
            }               
        }
        } catch (NoSuchMethodException nsme) {          
    }
于 2011-12-28T00:06:27.400 回答
1

尽管已经回答了这个问题,但我想添加更多方法来设置 overScrollMode 属性。

1) 创建一个“layout-v10”文件夹,并根据需要放置带有 overScrollMode 属性的备用 xml 布局。

2) 如果#1 意味着复制大量文件和重复项,您也可以为 Horizo​​ntalScrollView 创建一个样式。

于 2012-04-09T23:39:12.663 回答
1

您可以EdgeEffect使用反射设置颜色。以下内容适用于 API 14+:

public static void setEdgeGlowColor(final HorizontalScrollView hsv, final int color) {
    try {
        final Class<?> clazz = HorizontalScrollView.class;
        for (final String name : new String[] {
                "mEdgeGlowLeft", "mEdgeGlowRight"
        }) {
            final Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            setEdgeEffectColor((EdgeEffect) field.get(hsv), color);
        }
    } catch (final Exception ignored) {
    }
}

public static void setEdgeEffectColor(final EdgeEffect edgeEffect, final int color) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            edgeEffect.setColor(color);
            return;
        }
        final Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
        edgeField.setAccessible(true);
        final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
        glowField.setAccessible(true);
        final Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
        final Drawable mGlow = (Drawable) glowField.get(edgeEffect);
        mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mEdge.setCallback(null); // free up any references
        mGlow.setCallback(null); // free up any references
    } catch (final Exception ignored) {
    }
}
于 2015-01-20T20:52:00.157 回答