Question

I write my first app for Android and i have problem with view, properly with ImageView. I want show image cropped to width=100dp, which height=60dp. It works, width is equal 60dp, but height of ImageView is get from scaled image, not 60dp, so ListItem is higher or smaller than other one.

My XML look like this (this is list item):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#ccc">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/background"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"
    android:alpha="0.6"
    android:src="@drawable/little_walter_list"/>


<ImageView
    android:layout_width="100dp"
    android:layout_height="60dp"
    android:id="@+id/songImage"
    android:src="@drawable/little_walter_list"
    android:cropToPadding="false"
    android:layout_gravity="left"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="15dp"
    android:scaleType="centerCrop"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"

    android:text="Artist"
    android:id="@+id/artist"
    android:layout_gravity="center_horizontal"
    android:layout_alignTop="@+id/songImage"
    android:layout_toRightOf="@+id/songImage"
    android:textColor="#fff"
    android:paddingLeft="15dp"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="30dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Song name"
    android:id="@+id/title"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/artist"
    android:layout_toRightOf="@+id/songImage"
    android:textColor="#fff"
    android:textSize="15sp"
    android:paddingLeft="15dp"/>


</RelativeLayout>
Was it helpful?

Solution

you need to adjust the imageview bounds to be the same as the image

Just add android:adjustViewBounds="true" to your 'ImageView

<ImageView
    android:layout_width="100dp"
    android:layout_height="60dp"
    android:id="@+id/songImage"
    android:src="@drawable/little_walter_list"
    android:cropToPadding="false"
    android:layout_gravity="left"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="15dp"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top