Android 代码动态设置margin(报错:ViewGroup$MarginLayoutParams cannot be cast to android.widget.LinearLayout)

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
ViewGroup.MarginLayoutParams marginParams = null;
//获取view的margin设置参数
    if (params instanceof ViewGroup.MarginLayoutParams) {
        marginParams = (ViewGroup.MarginLayoutParams) params;
    } else {
//不存在时创建一个新的参数
//基于View本身原有的布局参数对象
marginParams = new ViewGroup.MarginLayoutParams(params);
    }
marginParams.setMargins(0,0,1,0);

 

千万记住这里面的if 语句的判断, 如果直接marginParams = new ViewGroup.MarginLayoutParams(params); 会报错

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: kodulf.swiperefreshlayoutrecyclerviewdemo, PID: 14673
    java.lang.ClassCastException: android.view.ViewGroup$MarginLayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:779)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22093)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6614)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22093)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6614)

 

ViewGroup$MarginLayoutParams cannot be cast to android.widget.LinearLayout 参考博文: (3条消息) Android 代码动态设置margin报错ViewGroup$MarginLayoutParams cannot be cast to android.widget.LinearLayout)_Kodulf的专栏-CSDN博客_viewgroup动态设置margin 错误代码 正确代码 val params: LayoutParams = child.layoutParams //获取viewmargin设置参数 v. 阅读详情

相关推荐

解决 Android 中的 ClassCastException:ViewGroup$MarginLayoutParams 无法转换为 android

为了解决这个问题,我们需要确保正确地使用适当的布局参数,并将其与正确的视图类型进行关联。通过遵循正确的布局参数类型和视图类型的配对,我们可以避免这个错误,并正确地操作视图和布局参数。为了解决这个问题,我们需要确保正确地使用适当的布局参数,并将它们与正确的视图类型进行关联。下面是一些可能导致这个错误的常见情况,以及如何修复它们的示例代码。在这种情况下,我们需要确保将布局参数对象转换为与其关联的正确视图类型。这个错误的原因是在代码中出现了类型不匹配的情况。在创建视图时,需要使用适当的布局参数类型。

OwxLinux的博客 328

Android 代码动态设置margin 报错ViewGroup$MarginLayoutParams cannot be cast to android.widget.RelativeLayout

ViewGroup.LayoutParams params = gridView.getLayoutParams(); ViewGroup.MarginLayoutParams marginParams = null; //获取viewmargin设置参数 if (params instanceof ViewGroup.MarginLayoutParams) { marginParams = (ViewGroup.MarginLayoutParams) params; }.

shenhe123的博客 1198

Android动态设置视图边距

Android应用程序开发中,动态设置视图的边距是一项常见的任务。通过在代码设置视图的边距,我们可以在运行时根据需要调整视图元素的位置和大小。本文将介绍如何在Android应用程序中使用Java代码动态设置视图的边距。通过使用上述代码,您可以轻松地根据需求调整视图的位置和大小,实现灵活的用户界面布局。通过使用上述代码,我们可以根据需要在运行时动态设置视图的边距。的子类,用于设置视图在父容器中的布局参数,包括边距信息。在上面的代码中,我们首先获取了视图的布局参数对象。类来设置视图的边距。

PixelEnigma的博客 394

ViewGroup$MarginLayoutParams cannot be cast to android.widget.RelativeLayout

);} else {));));} else {));));

chengzhenjia的博客 261

Android适配UI动态设置View视图控件的间距

一、动态设置View视图控件的间距如下布局,要动态控制TextView距离顶部的间距:1、需求:RelativeLayout有一个背景宽高为1635*1029px的图片,需要TextVie...

heiyl的专栏 3835

安卓自定义ViewGroup(加上了内外边距)

一、实现的效果图比较简单,主要是记录onMeasure()与onLayout加边距的使用 二、onMeasure(),仅仅是为了测量出当前自定义View的宽高,通过该宽高就能确定改View的显示的大小 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//这个方法测量出...

XiaoMu_Mu的博客 767

Android MarginLayoutParams问题记录

1 使用MarginLayoutParams来实现View跟随手指滑动的问题 如果搜索View滑动 百度搜出来的都是下面这段: LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLef

simple_a的博客 1351

自定义ViewGroup

一、自定义ViewGroup代码 自定义VIew的关键在于重写onMeasure和onDraw,而自定义ViewGroup的关键在于重写onLayout方法。 public class FlowLayout extends ViewGroup { //存储每行由哪些控件 List<List<View>> lineViews = new ArrayList(); //存储每行的高度 List<Integer> heightArray =

qq_41672484的博客 1376

android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

FATAL EXCEPTION: main Process: com.moreunion.zhenghao, PID: 3335 java.lang.ClassCastException: androi...

yuerliang的专栏 2878

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

项目场景: 在我实现自定义流式布局时,我使用了measureChildWithMargins对 问题描述: 提示:这里描述项目中遇到的问题: 例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据 APP 中接收数据代码: @Override public void run() { bytes = mmInStream.read(buffer); mHandler.obtainMessage(READ_DATA, bytes, -

lbj23vs6的博客 1433

Android自定义控件GroupViewMarginLayoutParams

1. MarginLayoutParams 是 ViewGroup.LayoutParams 的子类  2. 使用 我总结了下,一般我们会在哪些地方使用到 MarginLayoutParams 2.1 Activity/Fragment 这种界面代码中,我们会通过获取它来给View设置参数,比如我的这篇博客  Java设置View 的 Layout_Gravity 和 Gravity  这篇博客...

一抹离愁 1672

android中RecyclerView的简单使用(三),android系统开发视频教程

import java.util.ArrayList; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.View; import android.view.View.OnClickListener;

sa3221sa的博客 589

android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

LinearLayout layout = (LinearLayout) mInflater.inflate( R.layout.cell_check_item, null); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( ...

weixin_30830327的博客 389

android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

今天在实现动态改变布局的时候,报了个Error 仔细去看了下代码,发现自己真的太粗心大意了,记下来,方便日后翻阅。 原代码: ll_Pop_Params = ll_pop.getLayoutParams(); img_Product_Params = img_product_large.getLayoutParams(); ll_Pop_Params.width = (in

风华是一指流砂的专栏 2642

解决 android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

错误日志1: 06-13 10:55:50.410: E/KVLog(1129):Error info:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams06-13 10:55:50.423: E/KVLog(1129): Cause Result:java.lang.ClassCastExce...

dibe49481448的博客 485

Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object referen

E/AndroidRuntime: FATAL EXCEPTION: main Process: kodulf.swiperefreshlayoutrecyclerviewdemo, PID: 8661 java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$La...

Kodulf的专栏 9371

为什么都是ViewGroup的LayoutParams,也会报cannot be cast to android.view.ViewGroup$MarginLayoutParams

今天在代码里要动态改变 SurfaceView 的尺寸时,因为父布局是 FrameLayout ,自然就使用了 如下写法 val layoutParams = FrameLayout!!.layoutParams layoutParams.height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, resources.displayMetrics).toInt() layoutParams.width

知道的越多,不知道的越多。 1876
上一篇: Android使用WindowManager覆盖状态栏
下一篇: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object referen
千雅爸爸
博客等级 码龄17年 292粉丝 639原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值