<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent">
以上是布局文件代码,我想在代码中重新设置rl的宽度
RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.rl); RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(100, LayoutParams.MATCH_PARENT); relativeLayout.setLayoutParams(param);
就会报这个错 Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 。
解决方法:relativeLayout 是LinearLayout的子控件,它的LayoutParams 应该是LinearLayout给他的。所以应该是LinearLayout.LayoutParam 。如果relativeLayout 有子控件的话,那它的子控件用的的就是relativeLayout 给他们的,才是RelativeLayout.LayoutParam。