![](http://upload-images.jianshu.io/upload_images/1760078-0e283bd7897012cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540)
Paste_Image.png
蛋疼的问题,右边留空那么多。
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#ff0"
>
</android.support.v7.widget.RecyclerView>
item项的布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="wrap_content"
android:background="#f0f"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:layout_width="52dp"
android:layout_height="52dp"
app:srcCompat="@mipmap/home_new"
android:id="@+id/iv_icon" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#4A4A4A"
android:layout_marginTop="10dp"
android:id="@+id/tv_name" />
</LinearLayout>
设置GridLayoutManager 设置一行显示4个
GridLayoutManager gridLayoutManager = new GridLayoutManager(container.getContext(), 4);
recyclerView.setLayoutManager(gridLayoutManager);//一行显示4个
效果就成上面那样蛋疼了,经分析后,发现调整下item的根LinearLayout 的宽属性即可
android:layout_width="match_parent"
![](http://upload-images.jianshu.io/upload_images/1760078-a602f41b80f85f98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
Paste_Image.png
效果如下
![](http://upload-images.jianshu.io/upload_images/1760078-cd54a46cb769c553.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/409)
Paste_Image.png
若需要间距,就配置一下ItemDecoration
![](http://upload-images.jianshu.io/upload_images/1760078-72eceefe3d8faf16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/677)
Paste_Image.png
class MarginDecoration extends RecyclerView.ItemDecoration {
private int margin;
public MarginDecoration(Context context) {
margin = PxUtils.dpToPx(10,context);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(margin, margin, margin, margin);
}
}
作者:浩运
链接:https://www.jianshu.com/p/a6cd97d8646c
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。