蛋疼的问题,右边留空那么多。
<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"
效果如下
若需要间距,就配置一下ItemDecoration
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);
}
}