0

我正在尝试在我的 android 布局文件中创建一个 listview 布局。ListView 的结构是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Table" >

<ListView
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="launch_booklayout"
        android:text="@string/demo"
        android:textIsSelectable="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="launch_booklayout"
        android:text="@string/demo"
        android:textIsSelectable="true" />
</ListView>

我在 Eclipse 中得到以下错误:

列表/网格不应该有在 XML 中声明的子级

有谁知道我应该解决什么?谢谢

4

2 回答 2

1

正是错误所说的。改变

<ListView
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<ListView
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

并取出</ListView>. 现在,你有它不允许的TextViews作为孩子ListView

编辑

如果您正在扩展ListActivity,您的list. 你需要改变

<ListView
android:id="@+id/linearLayout1"
...

<ListView
android:id="@android:id/list"
...

如果您正在扩展,Activity那么您将不需要

于 2013-03-14T15:58:41.327 回答
0

你不能把任何孩子放在你的列表视图之间删除你的文本视图,它会解决你的问题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Table" >

<ListView
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="launch_booklayout"
    android:text="@string/demo"
    android:textIsSelectable="true" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="launch_booklayout"
    android:text="@string/demo"
    android:textIsSelectable="true" />

如果你想在你的列表视图中显示 2 个文本视图,你需要使用customAdapter类来实现这个目标

这是一个很好的列表视图教程: http ://www.vogella.com/articles/AndroidListView/article.html

于 2013-03-14T15:59:41.880 回答