web-dev-qa-db-ja.com

Android Lollipopにスクロールビューエッジカラー効果を追加するにはどうすればよいですか?

私のアプリでは、オーバースクロールグロー効果の色を次のように変更します。

int glowDrawableId = contexto.getResources().getIdentifier("overscroll_glow", "drawable", "Android");
Drawable androidGlow = contexto.getResources().getDrawable(glowDrawableId);
assert androidGlow != null;
androidGlow.setColorFilter(getResources().getColor(R.color.MyColor), PorterDuff.Mode.SRC_ATOP);

しかし、Lollipopに更新すると、このコードがクラッシュします。次のエラーコードが表示されます。

FATAL EXCEPTION: main
Process: com.myproject.myapp, PID: 954
Android.content.res.Resources$NotFoundException: Resource ID #0x0
at Android.content.res.Resources.getValue(Resources.Java:1233)
at Android.content.res.Resources.getDrawable(Resources.Java:756)
at Android.content.res.Resources.getDrawable(Resources.Java:724)

Lollipopにoverscroll_glowリソースがないようです。

どうすればこれを達成できますか?

15
user3065901

テーマでAndroid:colorEdgeEffectを指定して、アプリ全体のオーバースクロールグローの色を変更できます。デフォルトでは、これはAndroid:colorPrimaryによって設定された原色の値を継承します。

res/values/themes.xml:

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

または、インラインテーマオーバーレイを使用して、単一のビューのこの値を変更することもできます。

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" />
42
alanv

_"Android:colorEdgeEffect"_ソリューションは完全に機能し、以前のハックよりもはるかに優れています。ただし、エッジの色をプログラムで変更する必要がある場合は使用できません。

可能ですが、リフレクションを使用してこれを行うには、EdgeEffectオブジェクトをAbsListViewまたはScrollViewインスタンス。例えば:

_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() がLollipopに追加されました。

ただし、リフレクションベースのソリューションと同じ注意事項があります。

8
matiash

Lollipopでは、オーバースクロール効果の色をアイテムスタイルcolorPrimaryでカスタマイズできます。

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

この項目は、ツールバーの色にも影響します。

4
grunk

これを使用して、Android Lでプログラムでエッジの色を変更します。これはlistViewとscrollViewの両方で機能し、ビューはそれらを拡張します。

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

overscroll_glow.pngプラットフォーム21には存在しません。プラットフォーム20からリソースをコピーして使用できます。

発見できる overscroll_glow.png in:

{SDK_FOLDER}\platform\Android-20\data\res

このように、いくつかの更新後にプログラムを混乱させる可能性のあるリフレクションを使用しません。

1
Pedro Oliveira

手遅れであることはわかっていますが、これは私のアプリ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"/>
0
phoenix