【问题标题】:Set an image to whole of custom ListView将图像设置为整个自定义 ListView
【发布时间】:2015-12-30 14:39:57
【问题描述】:

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

谢谢你们。

【问题讨论】:

  • 这似乎已经回答here
  • 只是 .

标签: android android-listview android-xml


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    到这里

    android:background="@drawable/index_one"
    

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

    【讨论】:

    • @Beppe,非常感谢 :)
    【解决方案3】:

    只需将您的 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>
    ....
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        相关资源
        最近更新 更多