
<?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; }}基本上就是这样.如果只有一些背景图纸我也可以缓存它们,所以你不必一遍又一遍地阅读资源!
玩得开心!
总结以上是内存溢出为你收集整理的java – 如何在Android中为ListView的每个项目设置背景图像?全部内容,希望文章能够帮你解决java – 如何在Android中为ListView的每个项目设置背景图像?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)