我正在制作一个 Android 应用程序的原型,并尝试在编写任何 Java 代码之前定义 UI。基本的 XML 布局工作正常,但我不知道如何添加ListView
内容。添加ListView
里面 aLinearLayout
工作正常,但我不能添加任何东西(甚至不是另一个LinearLayout
)作为this的内容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.
XML 布局语言的 SDK 文档相当差,我似乎找不到任何明显的方法来完成这项工作。我错过了什么?
额外的问题:有没有办法在构建时检测 XML 布局中的问题,而不是等待 Android 在运行时抛出异常?