更改TapDown颜色上的ListView [英] Change TapDown Color on ListView

查看:106
本文介绍了更改TapDown颜色上的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,当用户点击向下的项目,它看起来是这样的。

I have a list view and when the user taps down on the item it looks like this.

这是什么样子noramally:

This is what it looks like noramally:

我要如何改变这个默认的淡蓝色?

How do I change this default light blue color?

我需要的颜色将在活动文件编程设置,因为它并不总是相同的颜色,还你如何改变在ListView的底部,如果您继续出现的模糊的东西颜色向下滚动?

I need the color to be set programatically in the activity file, as it is not always the same color, and also how do you change the color of the fuzzy stuff that comes up at the bottom of a ListView when you continue to scroll down?

感谢您的帮助。

修改

另外你怎么改变分接开关向下操作栏的颜色后退按钮,

Also how do you change the tap down color of the action bar back button,

推荐答案

有关该行的颜色,你需要创建一个的 StateListDrawable 和<一个href=\"https://developer.android.com/reference/android/widget/AbsListView.html#setSelector%28android.graphics.drawable.Drawable%29\"相对=nofollow>将其设置为选择作为的ListView

For the row color, you need to create a StateListDrawable and set it as the selector for the ListView.

该国将应至少包括:


  • pressed( android.R.attr.state_ pressed

  • 选择( android.R.attr.state_selected

  • 常规(空状态列表)

有关每个状态可以设置任何可绘制。如果你需要的是一个普通的颜色,可以以编程方式创建 Col​​orDrawable 当场。

For each state you can set any drawable. If what you need is a plain color, you can programmatically create a ColorDrawable on the spot.

例如:

StateListDrawable selector = new StateListDrawable();
ColorDrawable red = new ColorDrawable(Color.RED);
ColorDrawable transparent = new ColorDrawable(Color.TRANSPARENT);

selector.addState(new int[] { android.R.attr.state_pressed }, red);
selector.addState(new int[] { android.R.attr.state_selected }, red);
selector.addState(new int[] { }, transparent);

listView.setSelector(selector);


至于即在底部出现模糊的东西,这是稍微复杂一些。


As for the "fuzzy stuff that comes up at the bottom", it's slightly more complicated.


  • 要的是Android 5.0有要改变它没有公共API之前。

  • 在棒棒糖可以通过主题编程设置,但不能。

有关pre-5.0有一个众所周知的解决方法(的此处描述),其包括与所使用的可绘篡改。然而,这种解决方案在崩溃5.0,因为不存在了这些资源。

For pre-5.0 there is a well-known workaround (described here) which involves tampering with the drawables that are used. However this solution crashes in 5.0, because those resources do not exist anymore.

有关的是Android 5.0,如果静态颜色是可以接受的,你可以<一个href=\"https://stackoverflow.com/questions/27104521/android-lollipop-scrollview-edge-effect-color\">configure新的主题属性的android:colorEdgeEffect (这是一样的的android:colorPrimary 默认情况下)。

For Android 5.0, if a static color is acceptable, you can just configure the new theme attribute android:colorEdgeEffect (which is the same as android:colorPrimary by default).

如果为Android 5.0编程解决方案是必要的,你可以交替变化的ListView控件已经在内部使用反射 EdgeEffect 对象。我测试过这一点,它的工作原理,但它不是prettiest code:

If a programmatic solution for Android 5.0 is necessary, you could alternatively change the EdgeEffect objects that the ListView has internally using reflection. I've tested this and it works, though it's not the prettiest code:

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

因此​​,一个完整的编程解决方案可能会遇到这样的事情:

Therefore, a full programmatic solution may run something like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
    // For Android >= 5.0, use setColor() on EdgeEffect
    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();
    }
}
else
{
    // For Android < 5.0, change overscroll_glow and overscroll_edge
    int glowDrawableId = getResources().getIdentifier("overscroll_glow", "drawable", "android");
    Drawable androidGlow = getResources().getDrawable(glowDrawableId);
    androidGlow.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

    int edgeDrawableId = getResources().getIdentifier("overscroll_edge", "drawable", "android");
    Drawable androidEdge = getResources().getDrawable(edgeDrawableId);
    androidEdge.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); 
}

这篇关于更改TapDown颜色上的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆