自定义列表项到ListView android

前端之家收集整理的这篇文章主要介绍了自定义列表项到ListView android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在玩这个列表活动教程:

http://developer.android.com/resources/tutorials/views/hello-listview.html

它告诉你开始扩展List活动.

  1. by public class Main extends ListActivity {

这是基于对textview only布局进行充气.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:padding="10dp"
  6. android:textSize="16sp" >
  7. </TextView>

如果我想通过添加图像以及列表适配器上方的额外线性布局来更多地自定义布局,那么可以使用此方法 – 如果是这样,我该怎么做?

解决方法

可以使用 SimpleAdapter.

这是一个例子:

  1. // Create the item mapping
  2. String[] from = new String[] { "title","description" };
  3. int[] to = new int[] { R.id.title,R.id.description };

现在“title”映射到R.id.title,“description”映射到R.id.description(在下面的XML中定义).

  1. // Add some rows
  2. List<HashMap<String,Object>> fillMaps = new ArrayList<HashMap<String,Object>>();
  3.  
  4. HashMap<String,Object> map = new HashMap<String,Object>();
  5. map.put("title","First title"); // This will be shown in R.id.title
  6. map.put("description","description 1"); // And this in R.id.description
  7. fillMaps.add(map);
  8.  
  9. map = new HashMap<String,"Second title");
  10. map.put("description","description 2");
  11. fillMaps.add(map);
  12.  
  13. SimpleAdapter adapter = new SimpleAdapter(this,fillMaps,R.layout.row,from,to);
  14. setListAdapter(adapter);

这是相应的XML布局,这里名为row.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical">
  5. <TextView
  6. android:id="@+id/title"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:textAppearance="?android:attr/textAppearanceMedium" />
  10. <TextView
  11. android:id="@+id/description"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:textAppearance="?android:attr/textAppearanceSmall" />
  15. </LinearLayout>

我使用了两个TextView,但它对任何类型的视图都一样.

猜你在找的Android相关文章