java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
// xml文件
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
......
<ImageView
android:id="@+id/iv_settings"
android:onClick="startSetting"
android:layout_margin="10sp"
android:layout_gravity="bottom"
android:src="@drawable/home_larger_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
......
</FrameLayout>
ImageView mIvSettings = (ImageView) findViewById(R.id.iv_settings);
// 代码块
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
// FrameLayout mFl = (FrameLayout) mIvSettings.getParent();
params.gravity = Gravity.CENTER;
mIvSettings.setLayoutParams(params);
原因:因为mIvSettings的是包含在FrameLayout中的,如果要代码设置ImageView的属性的时候一定要new 对类型,比如此处一定要传FrameLayout.LayoutParams ,不然就会报上述错误