【问题标题】:Android Listview question:can not use <ListView> </ListView> in layout xml?Android Listview问题:布局xml中不能使用<ListView> </ListView>?
【发布时间】:2010-11-03 05:42:24
【问题描述】:

我尝试编写如下 xml 的 xml,但总是得到一个异常说 那“引起:java.lang.UnsupportedOperationException: Adapterview 不支持 addView(View,LayoutParams)”。 据说在布局xml中不能使用&lt;ListView&gt; &lt;/ListView&gt;,并且 它应该是&lt;ListView /&gt;并使用java代码进行操作,是吗?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">

    <ListView android:id="@+id/listview"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#00FF00"
              android:layout_weight="1"
              android:drawSelectorOnTop="false">

    <TextView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#000000"
              android:text="No data"/>
  </ListView>
</LinearLayout>

【问题讨论】:

    标签: android xml listview layout


    【解决方案1】:

    您不能以这种方式将TextView 放在ListView 内。您需要将ListAdapter 对象与ListView 相关联。

    【讨论】:

    • 你能扩展你的答案吗?我遇到了同样的问题,很难确定。
    • @BennettVonBennett:这是我的一本书的免费摘录,其中更详细:commonsware.com/excerpt.pdf
    【解决方案2】:

    最后,我发现滚动视图是我需要的。谢谢你的回复。 比如:

      <ScrollView>
       <LinearLayout>
       <TextView/>
       <TextView/>
       ...or something else
       <TextView/>
       </LinearLayout>
      </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      相关资源
      最近更新 更多