Listview问题与项目背景

时间:2012-05-04 18:32:14

标签: android background android-listview

我的ListView项目的背景有问题,它比应该更大。 这是我的背景资源:

enter image description here

它是我设备的屏幕截图:

enter image description here

布局:

    <?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>

这是我的项目布局:

    <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>

为什么背景如此之大?

2 个答案:

答案 0 :(得分:1)

由于您不希望图像缩放到列表项的尺寸,因此不应将其作为背景。
相反,你可以这样做。让背景为白色(因为您的car_bg资源全部为白色,带有一个徽标)。然后,您的listview布局可能如下所示:

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

这样您的图像就不会缩放并保持原始图像。也就是说,您现在必须自己购买不同尺寸的汽车。您必须为car_bg资源提供不同的密码图像。

答案 1 :(得分:0)

尝试scaling the ImageView to keep its aspect ratio,而不是尝试手动设置其高度/宽度。这很可能是您的ImageView似乎被拉长的原因。

相关问题