如何在线性布局 RecyclerView 中缩进分隔线(即,仅向 ItemDecoration 添加填充、边距或插图) [英] How to indent the divider in a linear layout RecyclerView (ie, add padding, margin, or an inset only to the ItemDecoration)
本文介绍了如何在线性布局 RecyclerView 中缩进分隔线(即,仅向 ItemDecoration 添加填充、边距或插图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照
Following this answer I was able to get a divider between the items of a vertical RecyclerView. However, I also wanted to slightly indent the divider lines.
I was able to do it by hard coding in an INDENT value in the RecyclerView.ItemDecoration
subclass.
int INDENT = 20;
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft() + INDENT;
int right = parent.getWidth() - parent.getPaddingRight() - INDENT;
// ...
divider.setBounds(left, top, right, bottom);
// ...
}
However, then I would have had to also mess with density independant pixels.
I finally found a solution similar to how it was done with ListView so I am sharing that as an answer below.
解决方案
Use inset
drawable/my_divider.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="40dp"
android:insetRight="40dp" >
<shape>
<size android:height="1dp"/>
<solid android:color="@color/recyclerview_divider" />
</shape>
</inset>
Using the constructor that takes a resource id as shown in this answer, we can supply the id of our custom divider xml file.
recyclerView.addItemDecoration(
new DividerItemDecoration(getActivity(), R.drawable.my_divider));
这篇关于如何在线性布局 RecyclerView 中缩进分隔线(即,仅向 ItemDecoration 添加填充、边距或插图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文