I have an imageview. I want its width to be fill_parent. I want its height to be whatever the width ends up being. For example:
I couldn't get David Chu's answer to work for a RecyclerView item and figured out I needed to constrain the ImageView to the parent. Set the ImageView width to 0dp
and constrain its start and end to the parent. I'm not sure if setting the width to wrap_content
or match_parent
works in some cases, but I think this is a better way to get the child of a ConstraintLayout to fill its parent.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="1:1"/>
</android.support.constraint.ConstraintLayout>
Maybe this will answer your question:
<ImageView
android:id="@+id/cover_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
You can ignore the scaleType
attribute for this particular situation.