开发者

Why is ADT calling code that the doc specifies throws an UnsupportedOperationException?

开发者 https://www.devze.com 2023-01-05 22:49 出处:网络
My XML layout in res/layout/edit.xml: <?xml version=\"1.0\" encoding=\"utf-8\"?> <GridView android:id=\"@+id/GridView01\"

My XML layout in res/layout/edit.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:t开发者_Python百科ext="Person's Points"
  android:id="@+id/PersonPoints"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
</GridView>

When I try to switch from "edit.xml" to "Layout", I see this error:

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

I checked the documentation for AdapterView, and this is expected behavior. Why, then, does ADT expect it to work?


It's because you're trying to add two TextViews to it in the xml. Don't do that. The views for any AdapterView have to come from the adapter.

You'll have to put your TextViews in another Layout that contains your GridView as well. Layouts you can use are: FrameLayout, LinearLayout, RelativeLayout, TableLayout, or implement your own. I think what you are trying to do is have two buttons on top of a GridView that you will be providing an Adapter that will fill it.

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

The two TextViews will be the same size because of the layout_weight attribute. The following GridView will take up the rest of the space.

If all you were trying to achieve was having the two TextViews next to each other but being the same size then just use the second LinearLayout with the horizontal orientation.

0

精彩评论

暂无评论...
验证码 换一张
取 消