蛋疼的問題古毛,右邊留空那么多肮塞。
<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>
設(shè)置GridLayoutManager 設(shè)置一行顯示4個
GridLayoutManager gridLayoutManager = new GridLayoutManager(container.getContext(), 4);
recyclerView.setLayoutManager(gridLayoutManager);//一行顯示4個
效果就成上面那樣蛋疼了,經(jīng)分析后梯澜,發(fā)現(xiàn)調(diào)整下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);
}
}