Android 如何设置两列recyclerview?
这些是我的布局文件,下面的图片是它们现在的样子,我想把这些图片分为两列Android 如何设置两列recyclerview?,android,kotlin,Android,Kotlin,这些是我的布局文件,下面的图片是它们现在的样子,我想把这些图片分为两列 如果我理解正确,您希望您的recyclerview中的项目能够显示在两列中。在设置layoutManager时尝试添加此项 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView a
如果我理解正确,您希望您的recyclerview中的项目能够显示在两列中。在设置layoutManager时尝试添加此项
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/PetPhoto_imageview"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintBottom_toTopOf="@+id/PetItem_textview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:srcCompat="@drawable/dog" />
<TextView
android:id="@+id/PetItem_textview"
android:layout_width="199dp"
android:layout_height="38dp"
android:layout_alignBottom="@+id/PetPhoto_imageview"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:alpha="0.3"
android:background="@color/black"
android:gravity="bottom"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
tools:text="100" />
</RelativeLayout>
您需要向您的RecyclerView添加一个范围计数为2的
例如:
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
从
xml
文件设置在RecyclerView
GridLayoutManager manager = new GridLayoutManager(context, 2);
recyclerview.setLayoutManager(manager);
我需要在与我的xml文件对应的活动下添加onCreate函数,对吗?是的,我认为您已经有了类似的功能,但在本例中是使用LinearLayoutManager而不是GridLayoutManager进行设置的。如果找不到它,请随时尝试使用您的java代码更新您的问题!快乐编码:)
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"