代码设置view的宽高注意细节

 <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

相关文章
相关标签/搜索