【问题标题】:ScrollView : Change the edge effect color with HoloScrollView : 用 Holo 改变边缘效果颜色
【发布时间】:2012-07-21 03:24:30
【问题描述】:

我目前正在尝试更改当您到达滚动视图的顶部或底部时出现的蓝色水平线的颜色。我试图在 Android res 文件夹中挖掘,但找不到任何明显的参考。

任何帮助将不胜感激。

谢谢。

更新:在尝试实现一个继承 ScrollView 的类并将 getSolidColor 设置为替代值后,它似乎不起作用。当我到达滚动视图的底部或顶部时出现的水平条仍然是蓝色的。

更新2:其实我不应该提到边缘效果颜色,更具体地说是overScroll效果,但我不知道这个词。

【问题讨论】:

标签: android styles themes scrollview


【解决方案1】:

这是更改过度滚动边缘线的方式:

        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);

【讨论】:

    【解决方案2】:

    您可以通过一些反射来更改ScrollViewEdgeEffect 颜色:

    public static void setEdgeGlowColor(final ScrollView scrollView, final int color) {
        try {
            final Class<?> clazz = ScrollView.class;
            final Field fEdgeGlowTop = clazz.getDeclaredField("mEdgeGlowTop");
            final Field fEdgeGlowBottom = clazz.getDeclaredField("mEdgeGlowBottom");
            fEdgeGlowTop.setAccessible(true);
            fEdgeGlowBottom.setAccessible(true);
            setEdgeEffectColor((EdgeEffect) fEdgeGlowTop.get(scrollView), color);
            setEdgeEffectColor((EdgeEffect) fEdgeGlowBottom.get(scrollView), color);
        } catch (final Exception ignored) {
        }
    }
    
    @SuppressLint("NewApi")
    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");
            final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
            edgeField.setAccessible(true);
            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) {
        }
    }
    

    【讨论】:

      【解决方案3】:

      我找到了一种非常简单(但很老套)的方法来完成此操作(更改发光颜色):

      int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
      Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
      androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);
      

      我利用了发光效果实际上是一个 Drawable 的事实(并且没有改变),并在其上应用了一个过滤器。 更多关于它,在这里:http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/

      【讨论】:

      • 太棒了。但是除了辉光之外,还有一条线出现在上方(向上滑动时)和下方(向下滑动时)。你也知道如何改变它的颜色吗?
      【解决方案4】:

      您可以使用边缘效果覆盖库来动态覆盖滚动视图上发光边缘的颜色。它使用起来非常简单,可以为您的应用程序添加一些独特性。在这里找到它:https://github.com/AndroidAlliance/EdgeEffectOverride

      【讨论】:

        【解决方案5】:

        我找到了我的问题的部分答案,我实际上指的是 ScrollView 的 overscroll 属性。似乎可以使用以下代码禁用它:

        <ScrollView
        ...
        android:overScrollMode="never"
        ... />
        

        但是,无法使用 overScrollHeader 和 overScrollFooter 属性修改颜色,因为它们的值会被忽略,而是显示默认的蓝色。

        【讨论】:

        • 对此有什么想法吗?
        • 不,我刚刚禁用了它们。
        • 这实际上不是答案。我想要事件本身,但需要禁用 ui 边缘效果)
        【解决方案6】:

        如果您希望渐变边缘和 ScrollView 的背景颜色相同,您可以添加:

        <ScrollView
        ...
        android:background="@color/yourColor"
        ... />
        

        如果您希望边缘颜色不同,则必须扩展 ScrollView 并覆盖 getSolidColor() 方法。

        【讨论】:

          【解决方案7】:

          do this using the api 似乎没有简单的方法。但是,您可以尝试扩展 ScrollView 并覆盖 getSolidColor() 方法,将背景设置为不同的颜色:

              new ScrollView(this) {
                  @Override
                  @ExportedProperty(category = "drawing")
                  public int getSolidColor() {
                      // Set a background color to a color you want for the edges.
                  }
              };
          

          【讨论】:

          • 我会尝试并给你一个反馈。奇怪的是你不能轻易做到这一点,因为这种蓝色不适合大多数应用程序主题。
          • 完成了,好像不行,蓝色的单杠还是蓝色的。
          猜你喜欢
          • 1970-01-01
          • 2011-02-08
          • 1970-01-01
          • 2018-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-13
          • 1970-01-01
          相关资源
          最近更新 更多