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」から「Layout」に切り替えようとすると、次のエラーが表示されます。

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

AdapterView のドキュメントを確認しましたが、これは予期された動作です。では、なぜ ADT はそれが機能することを期待しているのでしょうか?

4

1 に答える 1

1

TextViewこれは、xml で2 つの を追加しようとしているからです。そうしないでください。いずれかのビューはAdapterView、アダプターから取得する必要があります。

TextViewを含む別のレイアウトに を配置する必要がありますGridView。使用できるレイアウトは、FrameLayout、LinearLayout、RelativeLayout、TableLayout、または独自のものを実装することです。あなたがやろうとしているのは、GridViewそれを埋めるアダプターを提供する上に2つのボタンがあると思います。

<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>

属性により、 2 つTextViewの は同じサイズになりlayout_weightます。以下GridViewは、残りのスペースを占有します。

達成しようとしていたのが、2 つTextViewの s を隣り合わせにして同じサイズにすることだけだった場合は、2 つ目の s をLinearLayout水平方向に使用してください。

于 2010-07-01T19:52:08.917 に答える