Tus*_*ang 46 android android-cardview android-recyclerview
嗨,我正在按照本教程
http://www.journaldev.com/10024/android-recyclerview-and-cardview-example-tutorial
现在我面临一个奇怪的问题,回收者视图内的每个cardview项目之间的差距太大了.
问题
如何减少放置在回收站视图内的每个cardview项目之间的保证金.
Mah*_*bil 88
我遇到了类似的问题,RelativeLayout是recyclerview中每行的根元素.
要解决此问题,请找到包含每一行的xml文件,并确保根元素的高度为wrap_contentNOT match_parent.
小智 35
我上了一堂课来管理这个问题.此类为recyclerView中的项设置不同的边距:只有第一行具有上边距,只有第一列具有左边距.
public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
private final int columns;
private int margin;
/**
* constructor
* @param margin desirable margin size in px between the views in the recyclerView
* @param columns number of columns of the RecyclerView
*/
public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0) int columns ) {
this.margin = margin;
this.columns=columns;
}
/**
* Set different margins for the items inside the recyclerView: no top margin for the first row
* and no left margin for the first column.
*/
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildLayoutPosition(view);
//set right margin to all
outRect.right = margin;
//set bottom margin to all
outRect.bottom = margin;
//we only add top margin to the first row
if (position <columns) {
outRect.top = margin;
}
//add left margin only to the first column
if(position%columns==0){
outRect.left = margin;
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
您可以通过这种方式在Recyclerview中进行设置
RecyclerViewMargin decoration = new RecyclerViewMargin(itemMargin, numColumns);
recyclerView.addItemDecoration(decoration);
Run Code Online (Sandbox Code Playgroud)
Jin*_*ing 30
card_view:cardUseCompatPadding="true"在cards_layout.xml中找到该属性并将其删除.启动应用程序,您会发现每个cardview项目之间没有边距.
添加您喜欢的边距属性.例如:
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
Run Code Online (Sandbox Code Playgroud)小智 7
与其使用XML在RecyclerView中的项目之间增加边距,不如使用android框架提供的RecyclerView.ItemDecoration。
因此,我创建了一个库来解决此问题。
https://github.com/TheKhaeng/recycler-view-margin-decoration
小智 7
像这样CardView在Recyclerview项目布局中使用:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
card_view:cardCornerRadius="10dp"
app:cardBackgroundColor="#ACACAC"
card_view:cardElevation="5dp"
app:contentPadding="10dp"
card_view:cardUseCompatPadding="true">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92385 次 |
| 最近记录: |