【问题标题】:Background Images in Android ListView are not Showing UpAndroid ListView 中的背景图像未显示
【发布时间】:2014-03-18 17:17:48
【问题描述】:

我有一个自定义的ListView 和adapter,这是我的listview_item.xml 的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/background"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="17.0dip"
        android:fontFamily="sans-serif-condensed"
        android:paddingTop="17.0dip"
        android:textColor="@color/white"
        android:textSize="24.0dip" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/title"
        android:layout_marginLeft="18.0dip"
        android:layout_marginTop="-3.0dip"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#ffaaaaaa"
        android:textSize="13.0dip" />

    <Button
        android:id="@+id/btnRanks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Ranks" >
    </Button>

</RelativeLayout>

我想将图像设置为每个list 项目的背景,但是当我调用imageView.setImageDrawable(drawable); 时,我得到一个看起来像这样的listview:。您可以看到图像填充了listview 的整个width,但没有填充height。如何让图像也填充listview 的高度?

顺便说一下,这是预期的背景图片:

【问题讨论】:

    标签: android listview android-listview android-imageview android-xml


    【解决方案1】:

    在您的 ImageView 中,而不是

    android:layout_height="match_parent"
    

    使用

    android:layout_height="wrap_content"
    

    我也会删除该属性

    android:scaleType="centerCrop"
    

    【讨论】:

    • 嗯,listview 在更改后看起来完全一样。还有其他建议吗?
    • 如果所有项目的图像都相同...在xml布局中设置一次,而不是通过代码设置。
    • 不幸的是,每个列表项的图像都不同。这就是我以编程方式更改它的原因
    • 好的,这是有道理的。就像一个实验(在实践中不做):如果你强制高度与 px 中的图像一样高会发生什么?
    • 是的,但图像仍被裁剪/压缩。您还有其他潜在的解决方案吗?提前致谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多