简体   繁体   中英

Set Imageview Height according to ConstraintLayout height

I am trying to design layout in which Imageview height changes according to Constraint layout height.

So I tried to set constraint app:layout_constraintBottom_toTopOf="@id/tv_name" , app:layout_constraintTop_toTopOf="parent" and android:layout_height="0dp" to the Imageview but it's not working

It giving me 0dp height of Imageview as Output

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rv_item_home"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:paddingStart="0dp"
        android:paddingLeft="0dp"
        android:paddingEnd="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="4dp">


        <com.makeramen.roundedimageview.RoundedImageView
            android:id="@+id/img_item_home"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:scaleType="centerCrop"
            android:src="@drawable/logowithtext_trans"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/tv_name"
            app:riv_corner_radius="20dp" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="@string/name"
            app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintTop_toBottomOf="@id/img_item_home"
            />

        <ImageView
            android:id="@+id/img_share"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:padding="4dp"
            android:src="@drawable/ic_share_dark"
            app:layout_constraintTop_toBottomOf="@id/img_item_home"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>

Output Image 产量

Desired Image 所需的图像

you've limited the bottom of your RoundedImageView to top of the tv_name and bounded the top of tv_name to bottom of the RoundedImageView,the top constraint of tv_name isn't clear, that's why your image view can't detect the size. you can create a percent Guideline and limit your views to top and bottom of that.

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5"/>

or you can just add DimensionRatio to your roundImageView something like this:

<com.makeramen.roundedimageview.RoundedImageView
    android:id="@+id/img_item_home"
    app:riv_corner_radius="20dp"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:scaleType="centerCrop"
    android:src="@drawable/sample_img"
    app:layout_constraintDimensionRatio="H,3:1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<TextView
    android:id="@+id/tv_name"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/img_item_home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="name" />

Try to change constraint in RoundedImageView and TextView as below:

 <com.makeramen.roundedimageview.RoundedImageView
            android:id="@+id/img_item_home"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            ....
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/tv_name" />

<TextView
      android:id="@+id/tv_name"
      ....
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toBottomOf="@id/img_item_home"
      app:layout_constraintBottom_toBottomOf="parent" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM