0

我在 res/layout/edit.xml 中的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<GridView 
  android:id="@+id/GridView01"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:numColumns="2">
<TextView
  android:text="Person's Name"
  android:id="@+id/PersonName"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
<TextView
  android:text="Person's Points"
  android:id="@+id/PersonPoints"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
</GridView>

当我尝试从“edit.xml”切换到“布局”时,我看到了这个错误:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not
supported in AdapterView

我检查了 AdapterView 的文档,这是预期的行为。那么,为什么 ADT 期望它能够工作呢?

4

1 回答 1

1

这是因为您试图TextView在 xml 中添加两个 s 。不要那样做。任何人的意见AdapterView都必须来自适配器。

您必须将您TextView的 s 放在另一个包含您的布局中GridView。您可以使用的布局有:FrameLayout、LinearLayout、RelativeLayout、TableLayout,或实现您自己的。我认为您要做的是在一个按钮之上有两个按钮GridView,您将提供一个适配器来填充它。

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
        />
    </LinearLayout>
    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>

由于该属性,这两个TextViews 将具有相同的大小。layout_weight以下GridView将占用其余空间。

如果您想要实现的只是让两个TextViews 彼此相邻但大小相同,那么只需使用LinearLayout水平方向的第二个即可。

于 2010-07-01T19:52:08.917 回答