【问题标题】:How do I set a background image for each item of ListView in Android?如何为Android中ListView的每一项设置背景图片?
【发布时间】:2010-02-16 10:40:43
【问题描述】:
<?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 文件?我会把图像放在哪里?在资产中?

【问题讨论】:

    标签: java android listview templates


    【解决方案1】:

    如果您想为每个列表项设置单独的背景,您必须声明自己的自定义适配器。

    从 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;
        }
    }
    

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

    玩得开心!

    【讨论】:

    • 在适配器中保存一个列表或数组——无论你需要什么——并在适配器的构造函数中读取所有可绘制对象。然后,在设置背景时,不要在那里加载资源,而是使用列表或数组元素!但首先:不要关心这个。这只是我的一个附加提示,以防止您的适配器在浏览您的列表时变慢!祝你好运!
    • 如何制作可绘制的重复-x?在 CSS 中,我可以做 repeat-x :)
    • 嗯,你的意思是像瓷砖一样重复?我认为 setBackground 视图方法不可能做到这一点。视图有一个背景图像,就是这样。但我可能错了...... :-)
    【解决方案2】:

    您是否想要一个列表视图,其中每一行都有自己的背景,如下图所示?

    在您的代码中,您很可能会在视图上设置 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多