【问题标题】:Custom list item to ListView android自定义列表项到 ListView android
【发布时间】:2011-12-19 05:14:55
【问题描述】:

我一直在玩列表活动教程:

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

告诉你开始扩展 List 活动。

by public class Main extends ListActivity {

这是基于扩展仅 textview 的布局。

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

如果我想通过添加图像来自定义布局,以及在列表适配器上方的额外线性布局等-是否可以使用此方法-如果可以,我该怎么做?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    可以使用SimpleAdapter

    这是一个例子:

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

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

        // Add some rows
        List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
    
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title", "First title"); // This will be shown in R.id.title
        map.put("description", "description 1"); // And this in R.id.description
        fillMaps.add(map);
    
        map = new HashMap<String, Object>();
        map.put("title", "Second title");
        map.put("description", "description 2");
        fillMaps.add(map);
    
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
        setListAdapter(adapter);
    

    这是对应的XML布局,这里命名为row.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
    

    我使用了两个 TextView,但它适用于任何类型的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 2013-03-27
      • 1970-01-01
      • 2012-06-01
      • 2011-09-12
      • 1970-01-01
      相关资源
      最近更新 更多