<LinearLayout
android:id="@+id/ll_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > java
<ImageView
android:id="@+id/iv_move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/blue_line" /> android
</LinearLayout> 佈局
上面是佈局文件,我要早代碼中從新設置ImageView的寬度,用如下方法: spa
LayoutParams layoutParams = (LayoutParams) iv_move.getLayoutParams();
layoutParams.width = move_width;
iv_move.setLayoutParams(layoutParams); 3d
運行的時候會報錯: Caused by: java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.Re ci
解決方法:ImageView是LinearLayout的子控件,它的LayoutParams 應該是LinearLayout給他的。因此應該是LinearLayout.LayoutParam 。
get
代碼改爲: io
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv_move.getLayoutParams();
layoutParams.width = move_width;
iv_move.setLayoutParams(layoutParams); ast