public class RecyclerViewDivider extends RecyclerView.ItemDecoration {private Paint mPaint;private Drawable mDivider;private int mDividerHeight = 2;//分割线高度,默认为1pxprivate int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTALprivate static final int[] ATTRS = new int[]{android.R.attr.listDivider};/*** 默认分割线:高度为2px,颜色为灰色** @param context* @param orientation 列表方向*/public RecyclerViewDivider(Context context, int orientation) {if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {throw new IllegalArgumentException("请输入正确的参数!");}mOrientation = orientation;final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();}/*** 自定义分割线** @param context* @param orientation 列表方向* @param drawableId  分割线图片*/public RecyclerViewDivider(Context context, int orientation, int drawableId) {this(context, orientation);mDivider = ContextCompat.getDrawable(context, drawableId);mDividerHeight = mDivider.getIntrinsicHeight();}/*** 自定义分割线** @param context* @param orientation   列表方向* @param dividerHeight 分割线高度* @param dividerColor  分割线颜色*/public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {this(context, orientation);mDividerHeight = dividerHeight;mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setColor(dividerColor);mPaint.setStyle(Paint.Style.FILL);}//获取分割线尺寸@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);outRect.set(0, 0, 0, mDividerHeight);}//绘制分割线@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);if (mOrientation == LinearLayoutManager.VERTICAL) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}//绘制横向 item 分割线private void drawHorizontal(Canvas canvas, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getMeasuredWidth() - parent.getPaddingRight();final int childSize = parent.getChildCount();for (int i = 0; i < childSize; i++) {final View child = parent.getChildAt(i);RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getBottom() + layoutParams.bottomMargin;final int bottom = top + mDividerHeight;if (mDivider != null) {mDivider.setBounds(left, top, right, bottom);mDivider.draw(canvas);}if (mPaint != null) {canvas.drawRect(left, top, right, bottom, mPaint);}}}//绘制纵向 item 分割线private void drawVertical(Canvas canvas, RecyclerView parent) {final int top = parent.getPaddingTop();final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();final int childSize = parent.getChildCount();for (int i = 0; i < childSize; i++) {final View child = parent.getChildAt(i);RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + layoutParams.rightMargin;final int right = left + mDividerHeight;if (mDivider != null) {mDivider.setBounds(left, top, right, bottom);mDivider.draw(canvas);}if (mPaint != null) {canvas.drawRect(left, top, right, bottom, mPaint);}}}}

android中RecyclerView添加下划线相关推荐

  1. RecyclerView添加下划线

    RecyclerView添加下划线 最近写一个项目,要给RecyclerView添加下滑线,由于本人比较菜,所以去度娘上搜了一波,发现大神们的代码量真的好长(主要是我太懒),然后就想能不能在xml文件 ...

  2. 【Andrid】去掉android中EditText的下划线

    去掉android中EditText的下划线 android:background="@null"

  3. linux中文本怎么添加下划线,Android实现EditText添加下划线

    在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的! 控制如下: @color/colorPrimary @color/colorPrimaryDark **@color/col ...

  4. android 下划线edittext,Android实现EditText添加下划线

    在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的! 控制如下: @color/colorPrimary @color/colorPrimaryDark **@color/col ...

  5. Android 给EditText添加下划线

    在安卓高版本中,默认是有下划线的,其下划线的颜色是由其主题颜色来控制的 <style name="AppTheme" parent="Theme.AppCompat ...

  6. android中tabview去掉下划线,TabWidget去除底部下划线

    采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果.通常情况下会去除其下划线.如果是采用xml布局文件,在TabWidget的属性项设置a ...

  7. Swing中Laber添加下划线

    下划线的颜色可以单独定义 import java.awt.*; import javax.swing.*; import javax.swing.border.LineBorder;public cl ...

  8. 为文字添加下划线和中划线

    Android开发中如何给文字添加下划线和中划线呢,先看一下效果图: 下划线: 中划线: 方式一: //添加下划线 // textView.getPaint().setFlags(Paint.UNDE ...

  9. Android Textview 添加下划线 方法

    Android TextView 添加下划线的简单方法 在res的values资源文件夹下,给String.xml文件添加 <string name="userlicense" ...

  10. android textView 替文字添加下划线 删除线

    android textView 替文字添加下划线 删除线 方法1: tv=(TextView)findViewById(R.id.tv); tv.getPaint().setFlags(Paint. ...

最新文章

  1. fot mac matlab_matlab for mac的 安装-亲手实验
  2. Spring Cloud Security:Oauth2使用入门
  3. VMware Fusion 静态ip
  4. Python数据结构——栈、队列的实现(一)
  5. 如何在React中从其父组件更改子组件的状态
  6. Centos7 Docker 文件拷贝_入门试炼04
  7. C++ 同步/异步与阻塞/非阻塞的区别
  8. SteamVR 错误代码 108 / 203 / 208 / 301 / 306 / 308 / 400 / 405 排解方法
  9. Linux开发板启动需要登录密码设置方法
  10. [数值计算-11]:多元函数求最小值 - 偏导数与梯度下降法Python法代码示例
  11. 响应服务器530 5.7.0,SMTPSenderRefused(530,需要b'5.7.0身份验证)
  12. jsp显示中文文件名的图片 详细出处参考:http://www.jb51.net/article/37149.htm
  13. 流放者柯南自建服务器 linux,流放者柯南自建服务器教程一览服务器搭建方法介绍...
  14. 深度学习-Pytorch:项目标准流程【构建、保存、加载神经网络模型;数据集构建器Dataset、数据加载器DataLoader(线性回归案例、手写数字识别案例)】
  15. flickr的奇怪的认证过程
  16. Matlab --- 求解Ax=b时的反斜杠“\“,backslash
  17. 小米手机android评价,小米10优缺点简评
  18. 合伙人退出机制如何设定
  19. PYTHON练习题---IP判断
  20. java byte 图片浏览器直接显示_以Spring Boot的方式显示图片或下载文件到浏览器的示例代码...

热门文章

  1. 计算机主机闪烁显示器黑屏,显示器黑屏一闪一闪的怎么回事_显示器黑屏一闪一闪解决教程 - 系统家园...
  2. 北京机动车和自住房摇号查询
  3. 当开发人员说不是BUG时如何应对
  4. 品牌建设—长效且动态的关系管理进程:赫联电子亚太区
  5. 基于Java的Minecraft游戏后端自定义插件 08VexView界面绘制与按钮和扩展VexView事件使用
  6. ios vue 添加本地音乐_vue怎么添加音乐 vue怎么添加手机音乐
  7. 程序设计实践-21点赌博游戏
  8. java工作日志,如何写工作日志
  9. android系统平板输入法切换,安卓平板电脑怎么样切换输入法?
  10. 图像空间域和频域的分析