如何在Android中为ListView的每个项目设置背景图片?

4
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textStyle="bold"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/bottomtext"
            android:singleLine="false"
        />
    </LinearLayout>
</LinearLayout>

这是我的当前行。如果我创建了一个 .JPEG 文件,并且我希望每个项目都有该文件...我应该如何更改此 .xml 文件?我应该把图片放在哪里?在 Assets 文件夹中吗?

2个回答

3
如果您想为每个列表项设置单独的背景,您必须声明自己的自定义适配器。从BaseAdapter派生,并实现最重要的部分是getView(int, View, ViewGroup)方法。您必须了解Android在滚动列表时如何重新使用已经存在的列表项视图元素。也就是说,在任何时刻,只会生成与同时屏幕上可见的视图数量相同的视图。这种不生成太多视图的最佳策略导致了一个问题,即您将不得不为每个列表项根据其在调用getView时当前所需的位置设置背景。如果您尝试仅在生成视图时静态设置背景,则可能会再次出现,可能附加到错误的元素。getView方法会将“convertView”作为第二个参数带入,或者没有(null)。如果您的方法使用convertView设置为某些内容进行调用,那么意味着:“为当前所需的项目重复使用此视图”。这里使用的技术在API演示(列表部分)中得到了很好的描述,并且还有一个视频博客。以下是可能的实现方式:
public class MyAdapter extends BaseAdapter {

    public View getView(int position, View convertView, ViewGroup parent) {
        // position is the element's id to use
        // convertView is either null -> create a new view for this element!
        //                or not null -> re-use this given view for element!
        // parent is the listview all the elements are in


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.your_layout, null);

            // here you must do whatever is needed to populate the elements of your
            // list element layout
            ...
        } else {
            // re-use the given convert view

            // here you must set all the elements to the required values
        }

        // your drawable here for this element 
        convertView.setBackground( ... );

        // maybe here's more to do with the view
        return convertView;
    }
}

基本上就是这样了。如果只有几个背景图,我可能也会将它们缓存起来,这样你就不必一遍又一遍地读取资源了!

玩得开心!


在适配器中保持一个列表或数组 - 无论哪个更适合您的需求 - 并在适配器的构造函数中读取所有绘制对象。然后,在设置背景时,不要在那里加载资源,而是使用您的列表或数组元素! 但首先:不要担心这个。这只是我提供的一个附加提示,以防止您的适配器在浏览列表时变慢! 祝你好运! - Zordid
如何使drawable重复-x?在CSS中,我可以做到repeat-x :) - TIMEX
嗯,你的意思是像平铺一样重复吗?我不认为这可以通过视图的setBackground方法实现。视图只有一个背景图片,就是这样。但是我可能错了... :-) - Zordid

0
你想要一个列表视图,每一行都有自己的背景,就像下面这个例子吗?

alt text

在您的代码中,您很可能会在视图上设置一个ListAdapter。您可以构建的许多适配器都需要一个布局或视图参数。您只需为指向此视图的视图设置android:background属性即可。
例如,我可能会创建一个ArrayAdapter,如下所示:
new ArrayAdapter<String>(context, R.layout.item, R.id.itemName);

当然,我使用这个适配器的ListViewsetAdapter。在我的item.xml文件中,我可以像这样定义文本视图itemName
<TextView android:id="@+id/itemName" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Test view" android:background="@drawable/add"/>

然而,我认为并不是所有类型的ListAdapter都具有该功能的构造函数。请检查您正在使用的ListAdapter类型的文档。如果您使用的是没有这个构造函数的适配器,我认为您可能需要子类化适配器并覆盖适配器的getView方法,从您的XML文件中填充视图。

我在每个res/drawable文件夹中都有图像add.png。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接