[英]How to make List view for items in Android?
请帮我在这里。 我需要制作一个列表视图 ,如我的android应用程序的示例图片所示。 怎么做? 我还需要添加图像 ,如示例图片所示。 并将其链接到其他视图以显示更多详细信息。请检查下面显示的链接。
首先在layout / list_item.xml(或任何名称)中为列表项创建一个布局。 我只显示了该项目的文本字段,但是您可以更改下面的视图以同时包含图像和标签。
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:textSize="14dp"
a:paddingBottom="5dp"
a:paddingTop="5dp">
</TextView>
在另一个布局中定义一个listView,例如layout / list.xml
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:orientation="vertical"
a:stretchColumns="1">
<ListView a:id="@+id/paramList" a:layout_width="fill_parent"
a:layout_height="fill_parent" />
</LinearLayout>
在您的活动中,获取列表视图的句柄并向其中添加数据。
// Find the list view component in your layout.
ListView list = (ListView) findViewById(R.id.paramList);
// obtain data
List data = ... //some list data
// use a list adapter to render your list item in your list view!!
// The item is rendered using the list_item.xml layout.
// Here you can have any layout, with image and text as in your pic.
ListAdapter adapter = new ArrayAdapter(this, R.layout.list_item, data);
list.setAdapter(adapter);
您需要实现BaseAdapter 。 BaseAdapter定义了一个getView方法,该方法将返回列表项将使用的视图。 您可以以图表方式创建视图或从XML加载视图。 然后,在ListActivity中,使用setListAdapter方法。
在这里查看示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.