2012-09-21 51 views
2

我使用关闭按钮和幻灯片按钮加载web视图。当我CLICAndroid java.lang.ClassCastException

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

我想调整的一个按键,点击网页流量。

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

它工作!你是个天才!! – sam

+0

@ user113215,你为什么不给这个答案,所以山姆可以接受它? :) –

回答

1

正如Android Layouts API Guide指出,每种类型的ViewGroup有自己LayoutParams类定义孩子View S上的大小和位置。

而不是创建LayoutParams类之一的一个全新的实例,并冒着ClassCastException的,你可以打电话getLayoutParams()你的观点得到当前参数对象,如你所愿修改,并调用setLayoutParams()应用更新的参数。

相关问题