Android RecyclerView网格布局菜单不对齐,求正确修复方案
问题描述
我遇到了RecyclerView菜单项不对齐的问题,相关代码如下:
布局文件
RecyclerLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:background="#FF00FF"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/Recyclergrid" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" app:cardBackgroundColor="@color/app_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/Boxlayout" /> </LinearLayout>
CardLayout.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clipToPadding="false" android:layout_gravity="center" android:foreground="?selectableItemBackgroundBorderless" android:layout_marginBottom="20dp" android:id="@+id/root"> <androidx.cardview.widget.CardView android:id="@+id/CardLayout" android:layout_width="100dp" android:layout_height="150dp" app:cardCornerRadius="4dp" app:cardElevation="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/TextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/idIVcourseIV" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> </androidx.cardview.widget.CardView> <TextView android:id="@+id/TextView" android:layout_width="0dp" android:fontFamily="sans-serif" android:layout_height="wrap_content" android:ellipsize="end" android:minLines="2" android:maxLines="2" android:layout_marginTop="4dp" android:textColor="#FFFFFF" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Java 逻辑代码
Rcv= findViewById(R.id.Recyclergrid ); CardArray= new ArrayList<>(); CardArray.add(new DataModel("Text" ,"Title")); RecyclerAdapter RcvAdapter= new RecyclerAdapter(this, CardArray); Rcv.setLayoutManager(new GridLayoutManager(this, 3)); Rcv.setAdapter(RcvAdapter);
我尝试给RecyclerView添加gravity="center"属性未生效,目前通过给CardView加padding、居中RecyclerView的方式临时适配,求正确的修复方案。
修复方案
- 核心修改:调整
CardLayout.xml根布局的宽度属性,将android:layout_width="wrap_content"改为android:layout_width="match_parent"。GridLayoutManager会自动给每个item分配屏幕宽度1/3的可用区域,根布局占满分配区域后,内部已经配置了双向约束的CardView和TextView会自动在item区域内居中,自然解决对齐问题。 - 可选优化:如果需要统一调整item之间的上下左右间距,可给RecyclerView添加自定义ItemDecoration,示例实现如下:
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; private boolean includeEdge; public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { this.spanCount = spanCount; this.spacing = spacing; this.includeEdge = includeEdge; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = parent.getChildAdapterPosition(view); int column = position % spanCount; if (includeEdge) { outRect.left = spacing - column * spacing / spanCount; outRect.right = (column + 1) * spacing / spanCount; if (position < spanCount) { outRect.top = spacing; } outRect.bottom = spacing; } else { outRect.left = column * spacing / spanCount; outRect.right = spacing - (column + 1) * spacing / spanCount; if (position >= spanCount) { outRect.top = spacing; } } } }
使用时在设置LayoutManager之后添加即可:
// 可提前在dimens.xml中定义间距值,单位dp int spacing = getResources().getDimensionPixelSize(R.dimen.grid_spacing); Rcv.addItemDecoration(new GridSpacingItemDecoration(3, spacing, true));
内容的提问来源于stack exchange,提问作者yamete senpai
相关产品推荐
相关产品推荐

