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

时间:2015-12-30 14:39:58

标签: android android-listview android-xml

我正在制作一个消息应用。它工作正常。在对话活动中,我有一个自定义列表视图,用于扩展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>

谢谢你们。

4 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

布局中的

更改背景的属性,在这种情况下为ListView

到这个

android:background="@drawable/index_one"

你应该看到像这样的东西

enter image description here

答案 2 :(得分:0)

只需将ListView放在RelativeLayout中,高度匹配parrent,并为此布局设置图像设置背景。

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

答案 3 :(得分:0)

在您的XML中放置ImageView ListView以上就可以了。