Caused by: java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams

最近,在android中用代码动态改变某种布局(组件)的高度时,会遇到如题所示的类转换异常。上网查了一下,如下所示:

These supply parameters to theparentof this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

So basically, if you are adding a view to another, you MUST set the LayoutParams of the view to the LayoutParams type that the parent uses, or you will get a runtime error.

我是这样理解的,如果你要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。即要在代码中动态改变某组件的高度,其布局参数类型应该是其父类所使用的布局参数类型。
 比如:<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"  android:layout_height="wrap_content">    <FrameLayout android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>若想在代码中动态改变FrameLayout的大小,应该这样写:FrameLayout frameLayout=(FrameLayout) convertView.findViewById(R.id.FrameLayout01);   LinearLayout.LayoutParams ff=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height); 
frameLayout.setLayoutParams(ff); 注:其布局参数类型应该线性布局 LinearLayout.LayoutParams 的类型,而不是帧布局 FrameLayout .LayoutParams。

更多相关文章

  1. Andoroid学习笔记05——Activity布局管理
  2. android adb 命令实践
  3. Android中native_handle private_handle_t ANativeWindowBuffer
  4. android知识杂记(一)
  5. Android(安卓)通过包名打开App的代码
  6. Android(安卓)Material Design之NavigationView
  7. android 设置布局横屏竖屏
  8. Android(安卓)JNI 几个常用方法
  9. android选择图片或拍照图片上传到服务器(包括上传参数)

随机推荐

  1. Android(安卓)SmartRecyclerView
  2. Android二级分类列表GirdView
  3. Android(安卓)ADT 20.0.0 发布
  4. android(9)_数据存储与访问3_scard_login
  5. android 用到的技巧集
  6. Android中Handler的作用和使用方法
  7. android 编译 release 签名
  8. Understanding Android(安卓)Core: Loope
  9. android页面跳转实现
  10. Android(安卓)ProgressDialog 进度条对话