Tell me how to make the picture stretch in height. I stretched it in width, but I need it to be stretched in height while maintaining proportions. That's what I did and I do not understand how to fix it. enter image description here

The pictures themselves are 290px by 170px, created 3 columns and stretched them across the width, but with a height problem.

<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"> <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" /> <ProgressBar android:id="@+id/main_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> 

  • 3
    if these are items of the list, the layout of the item is needed, where these pictures are actually placed, and not the useless screen layout with a list - pavlofff

2 answers 2

For a list in which pictures in three columns, use a GridView. read here

and here you can read

    I repeat, I am new to java, I started putting it back somewhere, my attempts led to the next option for setting the height.

    main_content.xml

     <?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="ru.site.api.myapplication.MainActivity" tools:showIn="@layout/app_bar_main"> <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" /> <ProgressBar android:id="@+id/main_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </android.support.constraint.ConstraintLayout> 

    RecyclerView (@ + id / list) accepts items.xml

     <?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" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" app:cardBackgroundColor="@android:color/transparent" app:cardElevation="3dp" app:cardCornerRadius="3dp" app:cardPreventCornerOverlap="false"> <TextView android:id="@+id/alt_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20sp" android:textColor="@color/colorGray" android:text="@string/loading" /> <ru.site.api.myapplication.SquareImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> </android.support.v7.widget.CardView> </FrameLayout> 

    Class SquareImageView.java

     public class SquareImageView extends android.support.v7.widget.AppCompatImageView { public static final String d = SquareImageView.class.getCanonicalName(); public SquareImageView(Context context) { super(context); } public SquareImageView(Context context, AttributeSet attrs) { super(context, attrs); } public SquareImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // Ширину у высоту я знаю, поэтому проще. int heightPercent = 171*100/291; int height = getMeasuredWidth() / 100 * heightPercent; setMeasuredDimension(getMeasuredWidth(), height); } } 

    The result was the following: enter image description here

    If there is an option for easier, then tell me how to do it.