正如@Mohammed Atif 评论的那样,而不是使用
android:layout_toRightOf="@+id/message
用这个
android:layout_alignParentRight="true"
现在,要在 recyclerview 的项目之间添加空间,您需要添加 itemDecorator
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
outRect.bottom = mVerticalSpaceHeight;
}
}
}
然后像这样将这个项目装饰器添加到recyclerview
recyclerview.addItemDecoration(new VerticalSpaceItemDecoration(2));
这里,2是 recyclerview 列表项之间的空间。