问题: 自定义view MarginLayoutParams报错

本文介绍了一种解决自定义View中获取子View的Margin参数时遇到的问题的方法。通过检查并确保使用正确的LayoutParams类型,可以避免类初始化异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写自定义view时候获取子view 的Margin数据时候使用 

LayoutParams childLayoutParams = child.getLayoutParams();

             MarginLayoutParams marginLayoutParams  = (MarginLayoutParams) childLayoutParams;

报错,显示类初始化异常。

解决方法:

ViewGroup.LayoutParams params = child.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
                //获取view的margin设置参数
                if (params instanceof ViewGroup.MarginLayoutParams) {
                    marginParams = (ViewGroup.MarginLayoutParams) params;
                } else {
                    //不存在时创建一个新的参数
                    //基于View本身原有的布局参数对象
                    marginParams = new ViewGroup.MarginLayoutParams(params);
                }

判断是否存在,不存在就需要自己去创建

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package android.widget; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewDebug.ExportedProperty; public class TableRow extends LinearLayout { public TableRow(Context context) { super((Context)null); throw new RuntimeException("Stub!"); } public TableRow(Context context, AttributeSet attrs) { super((Context)null); throw new RuntimeException("Stub!"); } public void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener) { throw new RuntimeException("Stub!"); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { throw new RuntimeException("Stub!"); } protected void onLayout(boolean changed, int l, int t, int r, int b) { throw new RuntimeException("Stub!"); } public View getVirtualChildAt(int i) { throw new RuntimeException("Stub!"); } public int getVirtualChildCount() { throw new RuntimeException("Stub!"); } public LayoutParams generateLayoutParams(AttributeSet attrs) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateDefaultLayoutParams() { throw new RuntimeException("Stub!"); } protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } public CharSequence getAccessibilityClassName() { throw new RuntimeException("Stub!"); } public static class LayoutParams extends LinearLayout.LayoutParams { @ExportedProperty( category = "layout" ) public int column; @ExportedProperty( category = "layout" ) public int span; public LayoutParams(Context c, AttributeSet attrs) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h, float initWeight) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams() { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int column) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.LayoutParams p) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.MarginLayoutParams source) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { throw new RuntimeException("Stub!"); } } } 没有setMinWidth
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值