У меня есть recyclerview с ячейками динамической высоты. Высота ячейки зависит от длины текста.
Я хочу установить фоновое покрытие для этих ячеек. Высота ImageView должна быть такой же, как высота родительского вида. Удивительно, но выполнить эту задачу очень сложно.
Это макет результата без ImageView
Как видите, высота ячейки зависит от длины текста.
Макет результата с ImageView.
Как видите, высота ячейки зависит от высоты изображения.
<?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"
app:cardBackgroundColor = "#FF6B6B"
android:layout_marginBottom = "8dp"
android:layout_marginTop = "8dp"
android:layout_marginRight = "16dp"
android:layout_marginLeft = "16dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:cardCornerRadius = "8dp">
<FrameLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<ImageView
android:src = "@drawable/universe1"
android:id = "@+id/background"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:scaleType = "centerCrop">
</ImageView>
<LinearLayout
android:id = "@+id/layout_text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical">
<TextView
android:layout_marginLeft = "8dp"
android:textColorLink = "@color/white"
android:layout_marginBottom = "8dp"
android:id = "@+id/text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "2dp"
android:ellipsize = "end"
android:fontFamily = "@font/compact_text_regular"
android:maxLines = "4"
android:text = "This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
android:textColor = "@color/white"
android:textSize = "14sp"
android:lineSpacingExtra = "4dp" />
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "horizontal">
<LinearLayout
android:visibility = "gone"
android:id = "@+id/like_icon_wrap"
android:clickable = "true"
android:layout_gravity = "center"
android:layout_width = "40dp"
android:layout_height = "30dp"
android:orientation = "horizontal">
<ImageView
android:layout_marginLeft = "8dp"
android:id = "@+id/like_icon"
android:layout_gravity = "center_vertical"
android:layout_width = "16dp"
android:layout_height = "16dp"
app:srcCompat = "@drawable/heart_white" />
<TextView
android:id = "@+id/likes_count"
android:layout_gravity = "center"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:fontFamily = "@font/compact_text_regular"
android:textColor = "@color/white"
android:textSize = "16sp"
android:text = "0" />
</LinearLayout>
<TextView
android:layout_marginRight = "8dp"
android:layout_gravity = "center"
android:id = "@+id/more"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:fontFamily = "sans-serif"
android:textColor = "@color/colorPrimary"
android:gravity = "right"
android:text = "show more"
android:textSize = "12sp"
android:visibility = "invisible" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</androidx.cardview.widget.CardView>
Я также пытался установить высоту изображения из адаптера recyclerview, но безуспешно.
С помощью ConstraintLayout
вы можете сопоставить высоту другого вида, установив высоту на 0dp
и ограничив верх/низ другим видом, который вы хотите сопоставить.
Я вырезал другие части вашего макета, поэтому вам придется добавить их, но ниже приведен код для изображения и текста. Сделай это
<?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"
xmlns:tools = "http://schemas.android.com/tools"
android:id = "@+id/cardview"
app:cardBackgroundColor = "#FF6B6B"
android:layout_marginBottom = "8dp"
android:layout_marginTop = "8dp"
android:layout_marginRight = "16dp"
android:layout_marginLeft = "16dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:cardCornerRadius = "8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<ImageView
android:id = "@+id/background"
android:layout_width = "0dp"
android:layout_height = "0dp"
tools:src = "@tools:sample/avatars"
android:scaleType = "centerCrop"
app:layout_constraintStart_toStartOf = "@id/text"
app:layout_constraintEnd_toEndOf = "@id/text"
app:layout_constraintTop_toTopOf = "@id/text"
app:layout_constraintBottom_toBottomOf = "@id/text" />
<TextView
android:id = "@+id/text"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:ellipsize = "end"
android:lineSpacingExtra = "4dp"
android:maxLines = "4"
android:text = "This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
android:textSize = "14sp"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Правильный способ — использовать код @Håkon Schia. Но если вы хотите использовать FrameLayout, добавьте это внутри RelativeLayout, как это, и добавьте ImageView внутри этого с ограничением «alignBottom»:
<?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"
app:cardBackgroundColor = "#FF6B6B"
android:layout_marginBottom = "8dp"
android:layout_marginTop = "8dp"
android:layout_marginRight = "16dp"
android:layout_marginLeft = "16dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:cardCornerRadius = "8dp">
<!--
Code added here...
-->
<RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<ImageView
android:id = "@+id/background"
android:background = "@drawable/universe1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/frameLayout"
android:scaleType = "centerCrop">
</ImageView>
<FrameLayout
android:id = "@+id/frameLayout"
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<!--<ImageView
android:src = "@drawable/universe1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:scaleType = "centerCrop">
</ImageView>-->
<LinearLayout
android:id = "@+id/layout_text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical">
<TextView
android:layout_marginLeft = "8dp"
android:textColorLink = "@color/white"
android:layout_marginBottom = "8dp"
android:id = "@+id/text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "2dp"
android:ellipsize = "end"
android:fontFamily = "@font/compact_text_regular"
android:maxLines = "4"
android:text = "This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
android:textColor = "@color/white"
android:textSize = "14sp"
android:lineSpacingExtra = "4dp" />
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "horizontal">
<LinearLayout
android:visibility = "gone"
android:id = "@+id/like_icon_wrap"
android:clickable = "true"
android:layout_gravity = "center"
android:layout_width = "40dp"
android:layout_height = "30dp"
android:orientation = "horizontal">
<ImageView
android:layout_marginLeft = "8dp"
android:id = "@+id/like_icon"
android:layout_gravity = "center_vertical"
android:layout_width = "16dp"
android:layout_height = "16dp"
app:srcCompat = "@drawable/heart_white" />
<TextView
android:id = "@+id/likes_count"
android:layout_gravity = "center"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:fontFamily = "@font/compact_text_regular"
android:textColor = "@color/white"
android:textSize = "16sp"
android:text = "0" />
</LinearLayout>
<TextView
android:layout_marginRight = "8dp"
android:layout_gravity = "center"
android:id = "@+id/more"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:fontFamily = "sans-serif"
android:textColor = "@color/colorPrimary"
android:gravity = "right"
android:text = "show more"
android:textSize = "12sp"
android:visibility = "invisible" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>