![](/img/trans.png)
[英]How do I set the background color or image of a cell in a ListView (or ListActivity) on Android?
[英]How do I set a background image for each item of ListView in Android?
<?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文件? 我会把图像放在哪里? 在资产?
如果要为每个列表项创建单独的背景,则必须声明自己的自定义适配器。
从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;
}
}
基本上就是这样。 如果只有一些背景图纸我也可以缓存它们,所以你不必一遍又一遍地阅读资源!
玩得开心!
您是否想要一个列表视图,其中每一行都有自己的背景,如下所示?
在您的代码中,您很可能会在视图上设置ListAdapter 。 您可以构造的许多适配器包含布局或视图参数。 您应该只需为您指向的视图设置android:background属性。
例如,我可能有一个像这样创建的ArrayAdapter :
new ArrayAdapter<String>(context, R.layout.item, R.id.itemName);
当然,我使用ListView的setAdapter和这个适配器。 在我的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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.