be4code
be4code

Reputation: 698

Listview issue with item background

I've got problem with my ListView item's background, it's bigger than should be. Here is my background resource:

enter image description here

And it's screenshot from my device:

enter image description here

Layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_nocar">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="6.67dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reservation"
        android:textStyle="bold"
        style="@style/reservation_text" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reservation.1"
        style="@style/reservation_text" />
</LinearLayout>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/reservation.selectclass"
    style="@style/reservation_selectclass_text"
    android:layout_marginTop="33.33dp"
    android:layout_marginLeft="6.67dp" />
<ListView 
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#00000000"
    android:dividerHeight="0.67dp" />
</LinearLayout>

That's my item layout:

    <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reservation_row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/car_bg"
android:orientation="horizontal">
    <LinearLayout 
        android:layout_width="93.33dp"
        android:layout_height="98.67dp"
        android:orientation="vertical"
        android:layout_marginLeft="6.67dp"
        android:layout_marginTop="13.33dp">
        <TextView
            android:id="@+id/classname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/reservation_classname" />
        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/reservation_name"
            android:layout_marginTop="16.67dp" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="98.67dp"
        android:orientation="vertical"
        android:layout_marginLeft="6.67dp"
        android:layout_marginTop="13.33dp">
        <ImageView
            android:id="@+id/img"
            android:layout_width="150dp"
            android:layout_height="93.33dp"
            android:layout_marginTop="6dp" />
        <TextView
            android:id="@+id/price"
            android:layout_width="30dp"
            android:layout_height="20dp"/>      
        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    </LinearLayout>
</LinearLayout>

Why the background is so big?

Upvotes: 1

Views: 1185

Answers (2)

Urban
Urban

Reputation: 2199

Since you dont want your image to scale to the dimensions of the list item, you should not put it as a background.
Instead you can do this. Let the background be white (since your car_bg resource is all white with one logo). Then, your listview layout can look like this:

<RelativeLayout>  //background white
  <ImageView>  //Your car_bg with width and height set as wrap_content
  <LinearLayout>  //All the content that you previously had
</RelativeLayout>  

This way your image will not get scaled and remain as the original one. That said, you must now take car of different screen dimensions yourself. You must have different densitiy images available for your car_bg resource.

Upvotes: 1

Alex Lockwood
Alex Lockwood

Reputation: 83311

Try scaling the ImageView to keep its aspect ratio instead of attempting to set its height/width manually. This is most likely why your ImageView appears to be stretched.

Upvotes: 0

Related Questions