【问题标题】:Change TapDown Color on ListView更改 ListView 上的 TapDown 颜色
【发布时间】:2014-12-08 22:10:37
【问题描述】:

我有一个列表视图,当用户点击项目时,它看起来像这样。

正常情况下是这样的:

如何更改此默认浅蓝色?

我需要在活动文件中以编程方式设置颜色,因为它并不总是相同的颜色,以及当您继续时如何更改 ListView 底部出现的模糊内容的颜色向下滚动?

感谢您的帮助

编辑

还有如何改变操作栏后退按钮的点击颜色,

【问题讨论】:

    标签: android listview


    【解决方案1】:

    对于行颜色,您需要创建StateListDrawableset it as the selector for the ListView

    要包含的状态至少应该是:

    • 按下 (android.R.attr.state_pressed)
    • 已选中 (android.R.attr.state_selected)
    • “正常”(空状态列表)

    对于每个状态,您都可以设置任何可绘制对象。如果您需要的是纯色,您可以在现场以编程方式创建ColorDrawable

    例如:

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

    至于“底部出现的模糊的东西”,它稍微复杂一些。

    • 在 Android 5.0 之前,没有用于更改它的公共 API。
    • 在 Lollipop 中,可以通过主题设置,但不能以编程方式设置。

    对于 5.0 之前的版本,有一个众所周知的解决方法 (described here),其中涉及篡改所使用的可绘制对象。然而,该解决方案在 5.0 中崩溃,因为这些资源不再存在。

    对于 Android 5.0,如果可以接受静态颜色,则只需 configure the new theme attribute android:colorEdgeEffect(默认与 android:colorPrimary 相同)即可。

    如果需要针对 Android 5.0 的编程解决方案,您也可以使用反射更改 ListView 内部具有的 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();
    }
    

    因此,一个完整的程序化解决方案可能会像这样运行:

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

    【讨论】:

    • 我的目标是 16 及更高版本,所以我需要整个 if-else 部分吗?这段代码会去哪里?在我的 onCreate 中?
    • @iqueqiorio 是的,这是必要的。它可以去任何你可以访问ListView 实例的地方(可能是onCreate()onCreateView(),如果它是一个片段)。
    • 我将您代码的最后一块添加到我的onCreate() 中,但我看不出有什么区别,是不是我遗漏了什么?我在逻辑 Could not find method android.widget.EdgeEffect.setColor, referenced from method 中看到了这一点
    • @iqueqiorio 您需要以 API 级别 21 为目标才能使用该部分。
    • 我在我的 app.gradle targetSdkVersion 21compileSdkVersion 21 中有这个设置
    【解决方案2】:

    如何更改此默认浅蓝色?

    您必须创建StateListDrawable 并将其设置为selectorListView。 matiash的答案很好,我不想再解释了。

    更改出现在底部的模糊内容的颜色 继续向下滚动时的 ListView

    setCacheColorHint (int color)

    另外你如何改变操作栏的点击颜色 按钮

    <style name="actionbarCustomBackground" parent="@style/Theme.Holo.Light" >
        <item name="android:selectableItemBackground">@drawable/actionbar_item_background</item>
    </style>
    

    然后在actionbar_item_background中:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" 
        android:exitFadeDuration="@android:integer/config_mediumAnimTime">
        <item 
            android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@android:color/holo_orange_dark" />
        <item 
            android:state_pressed="true"
            android:drawable="@android:color/holo_orange_dark" />
        <item 
            android:drawable="@android:color/transparent" />
    </selector>
    

    【讨论】:

    • 你如何以编程方式执行操作栏点击颜色
    • 我认为您不能以编程方式执行此操作,因为正如我所搜索的那样,没有方法例如称为 setSelectableItemBackground
    【解决方案3】:

    您可以在getView(..) 方法中动态设置行的背景。

     public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
               .......... //your code
               view.setBackgroundColor(Color.RED)
    
    
            return view;
        } 
    

    【讨论】:

    • 我要设置onTapDown颜色
    猜你喜欢
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 2013-02-27
    • 2015-08-15
    相关资源
    最近更新 更多