2

閉じるボタンとスライド ボタンを含む Web ビューを読み込んでいます。クリックすると

main.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF" 
    android:layout_marginRight="25dip"
    android:layout_marginTop="25dip"
    android:layout_marginLeft="25dip"
    android:layout_marginBottom="25dip" >
<WebView
    android:id="@+id/webac_larger"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</WebView>

<ImageView android:id="@+id/close_btn"         
    android:layout_alignParentTop="true"         
    android:layout_alignParentRight="true"         
    android:layout_width="wrap_content"         
    android:layout_height="wrap_content"
    android:src="@drawable/close_button"
    android:layout_marginTop="-3dip"
    android:layout_marginRight="0dip"
    />  

<ImageButton 
    android:id="@+id/slidebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/close_btn"
    android:src="@drawable/arrowleft" />     

ボタンのクリックで WebView のサイズを変更したい。

final ImageButton button1 = (ImageButton) findViewById(R.id.slidebutton);
        button1.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            Log.d("MultiWeb", "I have clicked the button");
            RelativeLayout params = ((RelativeLayout )button1.getParent());
             params.setLayoutParams(new RelativeLayout.LayoutParams(100, 100));
          }
        });

このエラーが発生しています。relatice レイアウトを使用していますが、フレーム レイアウトに関するエラーが表示されるのはなぜですか?

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
4

1 に答える 1

1

Android Layouts API Guideに記載されているように、各タイプには、子のサイズと位置を定義するViewGroup独自のクラスがあります。LayoutParamsView

LayoutParamsいずれかのクラスの新しいインスタンスを作成して危険を冒す代わりに、ビューをClassCastException呼び出しgetLayoutParams()て現在のパラメーター オブジェクトを取得し、必要に応じて変更し、呼び出しsetLayoutParams()て更新されたパラメーターを適用できます。

于 2012-09-21T18:03:12.983 に答える