我原型的Android应用程序,并正尝试编写任何Java代码之前定义的UI。基本的XML布局的东西工作正常,但我无法弄清楚如何添加ListView
与内容。在LinearLayout
内添加ListView
可以正常工作,但我无法添加任何内容(甚至没有其他LinearLayout
)作为此ListView
的内容。如何用布局XML中的内容定义ListView?
我试着这样做的相当明显的方式:
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Line One"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Line Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Line Three"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ListView>
但尝试启动活动时有异常失败(在运行时):
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
的SDK文档XML布局语言相当糟糕,我似乎无法找到任何明显的方法来完成此工作。我错过了什么?
奖金问题:有没有办法在build-time的XML布局中发现问题,而不是等待Android在运行时抛出异常?
好的,知道了。感谢您的快速和彻底的答案! – kiwidrew