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-08-16 16:09:30

    android WebView 注入js 几种方式

    有时我们开发中需要将js 注入到我们本地,有可能你会说,放在Web不就可以了吗,的确,但是需求就是这样的

  • 2020-11-05 23:20:29

    mac更新node版本

    initializer function 0x0 not in mapped image for /usr/local/bin/node,除了上面的问题 你或许还出现过 no such file or directory 这样的提示,总之更新完以后node直接不能用了。

  • 2020-11-07 16:31:02

    nginx配置X-Frame-Options允许多个域名iframe嵌套

    有时候我们需要允许多个url的来源,但是又不能全部开放,所以应该匹配第三种方法ALLOW-FROM url,那么多个url该如何配置呢,百度了所有网站都没有找到,那么这里写给大家,其实很简单: add_header X-Frame-Options 'ALLOW-FROM https://xxx.xxxxxx.com https://xxx2.xxxxxxx.com'; 就是使用空格隔开就好了!

  • 2020-11-08 08:31:51

    meteor在不同端口启动服务

    当没有任何参数时,run是默认行为,在幕后,它3000端口开启node.js服务器实例,同时开启监听3001端口的MongoDB服务