栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view

java.lang.ClassCastException:android.view.ViewGroup$LayoutParams cannot be cast to android.view

项目场景:
在我实现自定义流式布局时,我使用了measureChildWithMargins对子view进行了测量,可是报错了

点开measureChildWithMargins的源码来看看

在这个方法中,拿到child的LayoutParams,然后强转为了MarginLayoutParams,这样就可以支持开发者在XML中给子view设置的margin值了,可是我们拿到的时普通的LayoutParams,而非MarginLayoutParams所有强转失败了,需要我们在viewgroup中去重写一个方法

override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
    return MarginLayoutParams(context, attrs)
}

每次子view拿到的就是一个MarginLayoutParams的实例,强转就不会失败了


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281672.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号