Я использую Recycler view
, который содержит Grid Layout
, чтобы представить элементы 2 на 2.
Проблема состоит в том, что первые два элемента не растягиваются соответственно с содержимым внутри. Для простоты объяснения вот один пример моей проблемы:
Что касается кода, в моем item.xml
у меня есть
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/dialog_background_color"
app:cardCornerRadius="10dp"
app:cardElevation="3dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_light">
<TextView
style="@style/SubtitleStyle"
android:id="@+id/vExpenseType"
android:textSize="15sp"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintTop_toTopOf="@+id/vExpenseImage"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/vExpenseImage"
app:layout_constraintStart_toEndOf="@+id/vExpenseImage"
android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp"/>
<ImageView
android:id="@+id/vExpenseImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_debt"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Это мой main.xml
, который содержит recycler view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
android:background="#CC000000"
tools:context=".ui.flows.main.fragments.overview.OverviewMainInputExpenseDialog">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/dialog_margin"
android:layout_marginStart="@dimen/dialog_margin"
android:background="@drawable/dialog_background"
android:transitionName="@string/transition_dialog"
android:orientation="horizontal">
<Button
android:id="@+id/close"
style="@style/ButtonLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:background="@drawable/btn_background"
android:text="Choose"
android:textColor="#fff"
android:textAppearance="?android:textAppearanceMedium"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/vMovementTypeRv"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/vMovementTypeRv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:spanCount="2"
tools:listitem="@layout/item_expense_type"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Тогда в моем соответствующем class
У меня есть:
vMovementTypeRv.layoutManager = GridLayoutManager(applicationContext,2)
Окончательные выводы
Я пытался изменить макет, но я не могу сделать эту работу. Может кто-нибудь показать мне какой-то вклад в это?