将图像设置为整个自定义 ListView

Set an image to whole of custom ListView

我正在制作一个消息传递应用程序。它工作正常。在对话 activity 中,我有一个自定义列表视图,它可以扩展 xml 布局,如下所示:

但我想放一张图片而不是像这样的绿色背景:

这是我现在的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#79ff93">

<ListView
    android:id="@+id/listMessages"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/divider"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:padding="0dip"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    tools:listitem="@layout/message_left" />

<RelativeLayout
    android:id="@+id/divider"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:layout_above="@+id/relSendMessage"
    android:background="@color/off_white" />

<RelativeLayout
    android:id="@+id/relSendMessage"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/white"
    android:paddingLeft="10dp">


    <EditText
        android:id="@+id/messageBodyField"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/sendButton"
        android:layout_alignTop="@+id/sendButton"
        android:layout_marginBottom="-4dp"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/sendButton"
        android:background="@android:color/white"
        android:hint="@string/message_hint"
        android:textColor="@android:color/black"
        android:textSize="14sp" />

    <Button
        android:id="@+id/sendButton"
        android:layout_width="72dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_margin="4dp"
        android:background="@drawable/button_send" />
</RelativeLayout>

谢谢大家。

只需将您的 ListView 放入高度匹配父级的 RelativeLayout 中,并为此布局所需图像设置背景。

....
<RelativeLayout
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/divider"
    android:background="@drawable/your_image" >

    <ListView
        android:id="@+id/listMessages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:padding="0dip"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/message_left" />

</RelativeLayout>
....

您可以使用可绘制对象作为背景。任何视图都可以获得可绘制的背景。对于您的情况,也许最好将其放在 ListView 上并从基本视图中删除 android:background 标记。

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chat_bg">

chat_bg 可以是可绘制文件夹中的 png 或 xml。 如果选择使用图像,则需要考虑不同的宽高比和屏幕尺寸。

在 XML 中的 ListView 上方放置一个 ImageView 就可以了。

在布局中更改背景的属性,在本例中为 ListView

到这个

android:background="@drawable/index_one"

你应该看到类似这样的东西