首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android上用xml定义静态元素的列表视图?

在android上用xml定义静态元素的列表视图?
EN

Stack Overflow用户
提问于 2012-12-14 00:31:10
回答 4查看 30.7K关注 0票数 40

我想创建一个片段,它将静态菜单显示为列表中的一组行。

我喜欢在表视图中使用静态单元格的iOS方法。我如何在android中实现这一点(所以不需要代码来定义元素,只需要xml)

接下来有没有什么常规的方法来定义xml中的静态元素?

(伪代码)

代码语言:javascript
复制
list_view.xml

<List view>
  - use element my_row with onclick=row1_clicked and title="row 1"
  - use element my_row with onclick=row2_clicked and title="row 2"
  - use element my_row with onclick=row3_clicked and title="row 3"
  - use element my_row with onclick=row4_clicked and title="row 4"
</List view>

my_row.xml

<My Row>
  - text field (title should go here)
  - on click (on click should go here)
</My Row>

所以基本上我想在列表中“包含”行,并在xml级别上(不需要代码)。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-17 22:42:17

不幸的是,禁止通过xml定义列表视图。相反,需要乱七八糟的适配器。

票数 26
EN

Stack Overflow用户

发布于 2014-05-27 14:24:28

可以使用字符串数组来取得一些进展,例如,

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/sports_array"/>
</LinearLayout>

与…结合

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="sports_array">
    <item>Shuttle Badminton</item>
    <item>Tennis</item>
    <item>FootBall</item>
  </string-array>
</resources>

来源:http://theopentutorials.com/tutorials/android/listview/android-creating-and-populating-listview-items-in-xml/

票数 24
EN

Stack Overflow用户

发布于 2013-07-09 01:13:49

您可以使用嵌套在ScrollView中的垂直LinearLayout来获得所需的内容。将您的" listview“项放入LinearLayout中,并使它们看起来像listview的元素。

如果您一定要使用ListView (例如,您正在使用ListFragment),那么您必须使用ListAdapter子类,或者使用您自己的子类。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13864260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档