Android中RecyclerView偏右,如何将其置于屏幕中央?
RecyclerView 偏右无法居中问题解决
问题描述
用于展示已保存卡片的RecyclerView始终偏向右方,无法水平居中,尝试设置marginLeft后问题仍未解决。
涉及布局代码
fragment_to_learn.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="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/flash_tablebar" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:layout_alignParentTop="true" tools:ignore="MissingConstraints"> <ImageView android:id="@+id/back_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:src="@drawable/ic_arrow_back" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </androidx.appcompat.widget.Toolbar> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" tools:listitem="@layout/recycle_view" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout>
recycle_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_margin="20dp" android:layout_marginTop="40dp" android:background="@drawable/rounded_cardview" android:layout_width="match_parent" android:layout_height="400dp"> <TextView android:layout_gravity="center" android:id="@+id/textView" style="@style/word_title" android:textColor="#FFFF" android:textStyle="bold" android:layout_marginStart="30dp" android:text="@string/word1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_marginLeft="20dp" android:layout_gravity="center" android:text="@string/word2" android:textColor="#FFFF" android:textStyle="bold" android:id="@+id/text1" style="@style/word_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageDelete" android:src="@drawable/baseline_delete_24" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" /> </LinearLayout>
当前显示效果

解决方法
1. 修复RecyclerView边距失衡问题
问题核心是RecyclerView仅设置了左侧边距,未配置右侧边距,导致左右空间不对称。修改RecyclerView的边距属性:
将android:layout_marginLeft="20dp"替换为:
android:layout_marginStart="20dp" android:layout_marginEnd="20dp"
配合已有的app:layout_constraintStart_toStartOf="parent"和app:layout_constraintEnd_toEndOf="parent"约束,RecyclerView即可在父布局中水平居中。
2. 优化Toolbar约束(可选)
Toolbar使用了RelativeLayout的android:layout_alignParentTop="true"属性,在ConstraintLayout中建议改用官方约束写法消除警告:
去掉android:layout_alignParentTop="true",添加:
app:layout_constraintTop_toTopOf="parent"
3. 统一Item布局边距规则(可选)
Item布局中android:layout_margin="20dp"与android:layout_marginTop="40dp"存在规则冲突,layout_marginTop会覆盖全局边距的顶部值,建议拆分明确设置:
android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_marginTop="40dp" android:layout_marginBottom="20dp"
内容的提问来源于stack exchange,提问作者PurpleRabbit
相关产品推荐
相关产品推荐

