Scrollview&RecycleView&ListView&Viewpager的顶部/底部阴影颜色改变

本文介绍了如何通过主题设置及反射方法更改Scrollview、RecycleView、ListView和Viewpager等组件顶部和底部阴影的颜色,适用于不同API级别的Android应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Scrollview&RecycleView&ListView&Viewpager的顶部/底部阴影颜色改变

0 利用主题设置


 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorEdgeEffect">@color/colorAccent</item>
    </style>

但是注意,只能在api21以上设置

示例

以下的方法为反射设置

1 Scrollview

牵扯到的两个属性mEdgeGlowTop,mEdgeGlowBottom,都为EdgeEffect类型

直接去反射改变颜色
这里以 mEdgeGlowTop 为例


Field topMethod = ScrollView.class.getDeclaredField("mEdgeGlowTop");
topMethod.setAccessible(true);
EdgeEffect top = (EdgeEffect) topMethod.get(scrollView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    top.setColor(Color.RED);
}

2 RecycleView

RecycleView有4个属性,mLeftGlow, mTopGlow, mRightGlow, mBottomGlow

采用和Scrollview 同样的方式既可以做到

3 ListView

listview是有一个public的方法的


    /**
     * Sets the drawable that will be drawn above all other list content.
     * This area can become visible when the user overscrolls the list.
     *
     * @param header The drawable to use
     */
    public void setOverscrollHeader(Drawable header) {
        mOverScrollHeader = header;
        if (mScrollY < 0) {
            invalidate();
        }
    }


    /**
     * Sets the drawable that will be drawn below all other list content.
     * This area can become visible when the user overscrolls the list,
     * or when the list's content does not fully fill the container area.
     *
     * @param footer The drawable to use
     */
    public void setOverscrollFooter(Drawable footer) {
        mOverScrollFooter = footer;
        invalidate();
    }

4 viewpager

Viewpager同样有两个属性,是左右的,反射方式同样适用 Scrollview的,属性为mLeftEdge,mRightEdge

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值