ImageView takes up too much vertical space

I am working on a project with a step-by-step gridview that can support a different height for each subview,

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/layout_border" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/holo_blue_light" /> </LinearLayout> 

as you can see in the pictures, for wide images ImageView takes up too much vertical space, this does not seem to be a problem for narrow pictures, How to fix it? VerticalHorizontal

+6
source share
4 answers

as Naven Tamrakar suggested, in StaggeredGridViewDemo they extended ImageView with the help of the ScaleImageView class, which solved the problem (not quite sure what the problem is and why ImageView does not wrap around the image, but this solution)

0
source

It has been a while, but for those who are dealing with this problem right now: android:adjustViewBounds="true" should do the trick!

+14
source

I had a similar problem and decided to override it onMeasure RelativeLayout method.

 package com.etsy.android.grid.util; import android.content.Context; import android.util.AttributeSet; import android.widget.RelativeLayout; public class DynamicHeightRelativeLayout extends RelativeLayout { public DynamicHeightRelativeLayout(Context context) { super(context); } public DynamicHeightRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public DynamicHeightRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Hack - This causes the height of RelativeLayout to match // it content when RelativeLayout is shown in StaggeredGridView. heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 

Then I used this class in the XML layout file:

 <?xml version="1.0" encoding="utf-8"?> <com.etsy.android.grid.util.DynamicHeightRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> </com.etsy.android.grid.util.DynamicHeightRelativeLayout> 
+1
source

You can use the ScaleType .. property for example:

  android:scaleType="centerCrop" 

There are several other types. Just choose the one that best suits your requirements.

-3
source

Source: https://habr.com/ru/post/974828/


All Articles