gpt4 book ai didi

android - 自定义列表项到 ListView android

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:38 25 4
gpt4 key购买 nike

我一直在玩这里的列表 Activity 教程:

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

它告诉您开始扩展 List Activity 。

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>

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

最佳答案

可以使用 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,但它适用于任何类型的 View 。

关于android - 自定义列表项到 ListView android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8554443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com