抱歉用词不当,我不知道 Android 术语到底是怎么回事,因此谷歌也帮不上什么忙。
我需要做的事情看起来很简单:我有一个表格作为我的“主视图”的一部分,它是在 Activity 启动时创建的。没问题,我可以用代码获取表格,例如添加行。
我想做的是在不同的 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 个字段、一个图像、两个字符串,并且它应该将图像“绑定(bind)”到属性缩略图和文本字段名字和姓氏?
最佳答案
看看ListActivity和/或 ListView .
编辑关于您的更新:使用SimpleAdapter而不是 ArrayAdapter
关于android - 在Android中创建 "Sub-Views"(新手问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5512256/