Android入门第七篇之ListView (二)

  Android入门第六篇之ListView (一) ,讲的是如何制作一个具有两行文本的 自定义控件 ,作为ListView的Item的使用方法。这篇接下来也是围绕ListView和Item,更加深入地介绍它们的用法。

       首先,先来看看本文代码运行的结果,本文的Item比上一篇中的Item多出左边的图标:


1


      main.xml的源代码,跟上一篇的一样,这里就不作解释了,直接贴出my_imageitem.xml的代码,就是它实现ImageItem的UI:

 

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout   
  3.          android:id="@+id/RelativeLayout01"   
  4.          android:layout_width="fill_parent"   
  5.          xmlns:android="http://schemas.android.com/apk/res/android"   
  6.          android:layout_height="wrap_content"   
  7.          android:paddingBottom="4dip"   
  8.          android:paddingLeft="12dip">  
  9.          <ImageView   
  10.                android:layout_width="wrap_content"   
  11.                android:layout_height="wrap_content"   
  12.                android:id="@+id/ItemImage">   
  13.          </ImageView>  
  14.          <TextView   
  15.                android:text="TextView01"   
  16.                android:layout_height="wrap_content"   
  17.                android:textSize="30dip"   
  18.                android:layout_width="fill_parent"   
  19.                android:layout_toRightOf="@+id/ItemImage"   
  20.                android:id="@+id/ItemTitle">  
  21.          </TextView>  
  22.          <TextView   
  23.                android:text="TextView02"   
  24.                android:layout_height="wrap_content"   
  25.                android:layout_width="fill_parent"   
  26.                android:layout_toRightOf="@+id/ItemImage"   
  27.                android:layout_below="@+id/ItemTitle"   
  28.                android:id="@+id/ItemText">  
  29.          </TextView>  
  30. </RelativeLayout>  

 

 

      解释一下 my_imageitem.xml的代码:这里使用了RelativeLayout布局,控件的关键的属性是:

ItemTitle的属性 android:layout_toRightOf="@+id/ItemImage" ,ItemTitle在ItemImage的右边;

ItemText的属性 android:layout_toRightOf="@+id/ItemImage",ItemText在ItemImage的右边, android:layout_below="@+id/ItemTitle", ItemText  ItemTitle的下面。

 

       最后,贴出JAVA的源代码,这里的源代码跟上一篇的很类似,只是修改了一部分,引入Item Image:

 

view plaincopy to clipboardprint?
  1. @Override  
  2.    public void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.        setContentView(R.layout.main);  
  5.        //绑定XML中的ListView,作为Item的容器  
  6.        ListView list = (ListView) findViewById(R.id.MyListView);  
  7.          
  8.        //生成动态数组,并且转载数据  
  9.        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
  10.        for(int i=0;i<10;i++)  
  11.        {  
  12.         HashMap<String, Object> map = new HashMap<String, Object>();  
  13.         map.put("ItemImage", R.drawable.icon);//添加图像资源的ID  
  14.         map.put("ItemTitle""This is Title.....");  
  15.         map.put("ItemText""This is text.....");  
  16.         lstImageItem.add(map);  
  17.        }  
  18.        //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应  
  19.        SimpleAdapter saImageItems = new SimpleAdapter(this//没什么解释  
  20.                                                 lstImageItem,//数据来源   
  21.                                                 R.layout.my_imageitem,//ListItem的XML实现  
  22.                                                   
  23.                                                 //动态数组与ImageItem对应的子项          
  24.                                                 new String[] {"ItemImage","ItemTitle""ItemText"},   
  25.                                                   
  26.                                                 //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
  27.                                                 new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});  
  28.        //添加并且显示  
  29.        list.setAdapter(saImageItems);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值