在Android中创建“子视图”(Newbie-Question)

时间:2011-04-01 10:42:22

标签: android


对于选择不当的单词感到抱歉,我不确切知道Android的术语是什么,因此谷歌也没有多大帮助。

我需要做的事情看起来很简单:我有一个表作为我的“主视图”的一部分,它是在活动启动时创建的。这里没问题,我可以用代码获取表格,例如添加行。

我想要做的是“准备”不同XML文件中的行,基本上使用XML创建布局,然后在代码中创建该TableRow的实例,并将其添加到我的主表中。

我知道另一种方法(在代码中创建整行),但是这对于大多数属性使用XML会更麻烦,并且只能通过代码(标签和图像)填充我以后需要的那些。 / p>

[UPDATE]

好的,我设法使用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个字段,一个图像,两个字符串,并且应该将图像“绑定”到属性Thumbnail和文本字段到名字和姓氏?

2 个答案:

答案 0 :(得分:1)

查看ListActivity和/或ListView

关于您的更新

编辑:使用SimpleAdapter代替ArrayAdapter

答案 1 :(得分:0)

类似于我理解你想做的例子:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

另一个每行输出两个字符串的例子:

WANTED: TableLayout-Like ListView

编辑:啊,我看到你找到了解决方案。

相关问题