android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutPara

2019-11-09 10:35:14

参考地址 android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutPara

<LinearLayout

            android:id="@+id/ll_card"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical" >

 

            <ImageView

                android:id="@+id/iv_move"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:background="@drawable/blue_line" />

 

            

        </LinearLayout>

上面是布局文件,我要早代码中重新设置ImageView的宽度,用以下方法:


LayoutParams layoutParams = (LayoutParams) iv_move.getLayoutParams();

layoutParams.width = move_width;

iv_move.setLayoutParams(layoutParams);


运行的时候会报错: Caused by: java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.Re




解决方法:ImageView是LinearLayout的子控件,它的LayoutParams 应该是LinearLayout给他的。所以应该是LinearLayout.LayoutParam 。


代码改成:





LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv_move.getLayoutParams();

layoutParams.width = move_width;

iv_move.setLayoutParams(layoutParams);



  • 2020-11-17 09:50:32

    android开发怎样让悬浮Activity只是隐藏而不销毁

    android在mainfest中给Activity添加一个属性 android:theme="@android:style/Theme.Dialog",可以使Activity悬浮在其它窗口上面,在布局中可以设置activity的大小,当点击悬浮Activity边缘以外区域时,Activity会消失,观察消失时其生命周期会发现执行了finish()方法从而执行了onDestroy方法。有时我们只是需要将Activity隐藏,并不销毁,此时可以重写finish方法,如下:

  • 2020-11-17 09:56:02

    uni-app直接用webiew打开本地js资源

    如果再结合activity不销毁,隐藏的方法,像里面传递参数,来改变页面,不销毁webview,我发现这样比原生的都要快。这样又能用于app端,又能生成小程序,何乐而不为

  • 2020-11-17 09:59:05

    在线图片取色器工具

    不知道为啥,现在idea的取色工具怎么都不好使了,给开发人员反映过,都一年了有没修复咋地。 网上搜到一个取色工具,把图片传上去,就可以点击取色。不错。

  • 2020-11-17 10:05:53

    mongoDB 修改密码

    mongodb的用户信息是存放在system.users表中的,修改密码不能直接更新表数据,这样的话,存到表里的密码是明文的,这就不对了。