我試圖在我的 recyclerView 中水平對齊我的 cardViews,但是當我更改 XML 中的方向屬性時,沒有任何變化。有誰知道還涉及哪些其他因素?配接器或 card_layout 檔案是否有問題?
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".UI.Game.GuessWordFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="36dp"
android:layout_marginBottom="137dp"
android:orientation="horizontal"
android:scrollbars="horizontal"
app:layout_constraintBottom_toTopOf="@ id/inputField"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/CategoryText" />
<EditText
android:id="@ id/inputField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Type your guess"
android:imeOptions="actionDone"
android:inputType="textPersonName"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/recyclerView" />
<Button
android:id="@ id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/inputField" />
<TextView
android:id="@ id/CategoryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@ id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
創建 layoutManager 類時,請使用 LinearLayoutManager.HORIZONTAL
val layoutManager
= LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
binding.recyclerview.setLayoutManager(layoutManager)
uj5u.com熱心網友回復:
如果你想要 recyclerview 對齊垂直
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
而對于水平
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
只需在您的代碼中添加這一行...謝謝...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371507.html
