ImageView - have height match width?

后端 未结 14 2416
长发绾君心
长发绾君心 2020-12-02 05:17

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:



        
相关标签:
14条回答
  • 2020-12-02 06:13

    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>
    
    0 讨论(0)
  • 2020-12-02 06:14

    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.

    0 讨论(0)
提交回复
热议问题