android lollipop scrollview edge effect color

点点圈 提交于 2019-11-28 17:59:20

You can specify android:colorEdgeEffect in your theme to change the overscroll glow color within your entire app. By default, this inherits the primary color value set by android:colorPrimary.

res/values/themes.xml:

<style name="MyAppTheme" parent="...">
    ...
    <item name="android:colorEdgeEffect">@color/my_color</item>
</style>

Alternatively, you can modify this value for a single view using an inline theme overlay.

res/values/themes.xml:

<!-- Note that there is no parent style or additional attributes specified. -->
<style name="MyEdgeOverlayTheme">
    <item name="android:colorEdgeEffect">@color/my_color</item>
</style>

res/layout/my_layout.xml:

<ListView
    ...
    android:theme="@style/MyEdgeOverlayTheme" />

The "android:colorEdgeEffect" solution works perfectly, and is much better than the previous hacks. However, it cannot be used if the edge color needs to be changed prorgrammatically.

It is possible, though, to use reflection to do so, setting the EdgeEffect objects directly in the AbsListView or ScrollView instances. For example:

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

EdgeEffect.setColor() was added in Lollipop.

Same caveats as any reflection-based solution, though.

In lollipop the overscroll effect color can be customized with the item style colorPrimary :

<style name="MyApp" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/mycolor</item>
</style>

This item also affect the color of the toolbar.

I'm using this to change the edge color programmatically on android L. This works for both listView and scrollView, and views extend them.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void setEdgeEffectL(View scrollableView, int color) {
    final String[] edgeGlows = {"mEdgeGlowTop", "mEdgeGlowBottom", "mEdgeGlowLeft", "mEdgeGlowRight"};
    for (String edgeGlow : edgeGlows) {
        Class<?> clazz = scrollableView.getClass();
        while (clazz != null) {
            try {
                final Field edgeGlowField = clazz.getDeclaredField(edgeGlow);
                edgeGlowField.setAccessible(true);
                final EdgeEffect edgeEffect = (EdgeEffect) edgeGlowField.get(scrollableView);
                edgeEffect.setColor(color);
                break;
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
            }
        }
    }
}

overscroll_glow.png doesn't exist in platform 21. You can copy the resourses from platform 20 and use them.

You can find overscroll_glow.png in:

{SDK_FOLDER}\platforms\android-20\data\res

This way you don't use reflection that can, as you can see, mess with your program after some updates.

i know i am too late, but this works for me for my app api >=17:

<style name="ListTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="colorPrimary">@color/colorPrimaryDark</item>
</style>

<ListView   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ListTheme"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!