ItemDecoration自定义宽度和颜色

本文介绍了一种在RecyclerView中实现垂直布局分割线的方法。通过自定义绘制逻辑,利用Canvas和Drawable来绘制分割线,并调整其宽度和颜色。具体包括计算分割线的位置、设置颜色过滤器等步骤。
/**
* 垂直布局分割线绘制
* @param c
* @param parent
*/
private void drawVertical(Canvas c,RecyclerView parent){
final int left=parent.getPaddingLeft();
final int right=parent.getWidth()-parent.getPaddingRight();
final int childCount=parent.getChildCount();
for(int i=0;i<childCount;i++){
final View child=parent.getChildAt(i);
final RecyclerView.LayoutParams params= (RecyclerView.LayoutParams) child.getLayoutParams();
final int top=child.getBottom()+params.bottomMargin+Math.round(ViewCompat.getTranslationZ(child));
final int bottom=top+drawable.getIntrinsicHeight();
drawable.setBounds(left,top+2,right,bottom);//宽度调节
drawable.setColorFilter(context.getResources().getColor(R.color.colorAccent), PorterDuff.Mode.XOR);//Mode选择不对无法正常显示选择的颜色
drawable.draw(c);


如代码所示,宽度其实就是控制top和bottom的距离,加减就可以了,很简单。
颜色就是drawable设置setColorFilter设置颜色和Mode,注意有的Mode颜色显示不正常

转载于:https://www.cnblogs.com/a1070022846/p/7766654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值