很抱歉用词不当,我不知道Android的术语是什么,因此谷歌也帮不了太多忙。
我需要做的似乎很简单:我有一个表作为我的“主视图”的一部分,该视图是在活动启动时创建的。这里没有问题,我可以在代码中获取表格,例如添加行。
我想要做的是“准备”不同的XML文件中的行,基本上是使用XML创建布局,然后在代码中创建该TableRow的实例,将其添加到我的主表中。
我知道另一种方法(用代码创建整行),但这比使用XML处理大多数属性要麻烦得多,而且只用代码填充我稍后需要的属性(标签和图像)。
更新
好的,我设法用ListActivity解决了一半的问题。还有一个小问题,我不知道如何告诉Android使用多个字符串。
代码如下所示:
public class ListScreenAlle extends ListActivity {
String[] listItems = {"exploring", "android", "list", "activities"};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.list_screen_layout_alle);
//setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
setListAdapter(new ArrayAdapter(this, R.layout.list_row_single, listItems));
}}
用于该行的XML如下所示:
<?xml version="1.0" encoding="utf-8"?><TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textColor="@color/text_color_list_label"/>如何更改此代码以支持比单个字符串更复杂的对象。有缩略图和名、姓的人
简单形式的ArrayAdapter只允许一个文本字段,我该如何向它解释我的行有3个字段、一个图像、两个字符串,以及它应该将图像“绑定”到属性缩略图,并将文本字段绑定到名字和姓氏?
发布于 2011-04-01 19:01:25
看看ListActivity和/或ListView。
关于您的更新的编辑:使用SimpleAdapter代替ArrayAdapter
发布于 2011-04-01 20:08:22
类似于我所理解的你想要做的例子:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
另一个每行输出两个字符串的示例:
WANTED: TableLayout-Like ListView
编辑:啊,我看你找到了一个解决方案。
https://stackoverflow.com/questions/5512256
复制相似问题