android - 将背景图像添加到 Android 中的 ListView ?

标签 android android-layout

我正在尝试将背景图像添加到我的应用程序中的 listActivity。但是,在我使用的代码中,背景图像显示在每一行而不是整个列表中。

这是 xml 文件:

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

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

<TextView
    android:id="@+id/employeeID"
    android:layout_width="50px"
    android:layout_marginRight="20px"
    android:layout_height="wrap_content"
    android:textColor="@color/textColor"/>

<TextView
    android:id="@+id/employeeName"
    android:layout_width="wrap_content"
    android:layout_marginRight="10px"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/employeeID"
    android:textColor="@color/textColor"/>


</RelativeLayout>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

向 Activity 添加背景图像的最佳方法是什么?我宁愿在 XML 文件中而不是以编程方式执行。

最佳答案

我通过将图像背景添加到主视图然后在该 View 中添加 listView 解决了这个问题。

所以这是我的 main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="4px"
android:background="@drawable/qbg"> 

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
             android:cacheColorHint="#00000000"/> //This is to fix disappearing background issue
</RelativeLayout>

这是我的 listView.xml

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

<TextView
android:id="@+id/employeeID"
android:layout_width="50px"
android:layout_marginRight="20px"
android:layout_height="wrap_content"
android:textColor="@color/textColor"/>

<TextView
android:id="@+id/employeeName"
android:layout_width="wrap_content"
android:layout_marginRight="10px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/employeeID"
android:textColor="@color/textColor"/>

</RelativeLayout>

希望这对其他人有益。

关于android - 将背景图像添加到 Android 中的 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086857/

相关文章:

安卓 : How to change name of the Enter key to "Search" with respect to particular EditText. ..?

Android:如何检测滚动何时结束

android - 在 xml 布局中包含类时,Eclipse 查看器无法实例化 com.mopub.mobileads.MoPubView

android - 连续两次调用 Activity.setContentView() 的副作用

android - 可扩展 ListView 底部分隔线

android - 在方向更改时更改 fragment 布局

android - 错误 : Inflating class com. google.android.material.textfield.TextInputLayout

android - tesseract 数据路径不存在

android - 使用 "Theme.of"会导致在小部件上重新构建

android - 错误 :Execution failed for task ':app:packageDebug' - Java heap space