ListView的headerView自定义布局,运行报错:
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
如果要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。即要在代码中动态改变某组件的高度,其布局参数类型应该是其父类所使用的布局参数类型。
ListView的headerView的LayoutParams参数改为和ListView一致:
ListView.LayoutParams LP=new ListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
本文介绍了解决在Android开发中遇到的ListView HeaderView布局参数类型错误的问题。当尝试自定义HeaderView布局并将其添加到ListView时,若使用了不正确的LayoutParams类型,会导致运行时报错。文章提供了一个简单的解决方案,即将LayoutParams类型更改为与ListView相匹配。
14万+

被折叠的 条评论
为什么被折叠?



