您的位置:

react-native有IllegalArgumentException("Cannot get layout margins of multi-edge shorthands")报错是怎么回事

  发布时间:2023-01-23 20:30:02
报错的原因这个错误通常是由于在Java代码中使用了React Native中不支持的布局属性导致的。将"margin"替换为和"marginRight"即可。使用例子以下是一个使用React Native在Java代码中设置视图外边距的示例错误代码正确代码或者这样就能成功设置外边距,并避免出现错误。

报错的原因

这个错误通常是由于在Java代码中使用了React Native中不支持的布局属性导致的。例如,使用了"margin"而不是"marginTop"、"marginBottom"、"marginLeft"和"marginRight"。在React Native中,这些属性是不受支持的,因此会导致IllegalArgumentException。

如何解决

解决这个错误的方法是更改Java代码中使用的布局属性。将"margin"替换为"marginTop"、"marginBottom"、"marginLeft"和"marginRight"即可。

例如:


// 错误代码
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 20, 20, 20);

// 正确代码
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 20, 20, 20);

或者


ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
((ViewGroup.MarginLayoutParams) params).leftMargin = 20;
((ViewGroup.MarginLayoutParams) params).rightMargin = 20;
((ViewGroup.MarginLayoutParams) params).topMargin = 20;
((ViewGroup.MarginLayoutParams) params).bottomMargin = 20;

这样就能解决这个问题了。

使用例子

以下是一个使用React Native在Java代码中设置视图外边距的示例:


import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MyViewManager extends ViewGroupManager {

  @Override
  public String getName() {
    return "MyView";
  }

  @Override
  protected LinearLayout createViewInstance(ThemedReactContext reactContext) {
    LinearLayout view = new LinearLayout(reactContext);

    // 错误代码
    // ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    // params.setMargins(20, 20, 20, 20);

    //正确代码
    ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(20, 20, 20, 20);
    view.setLayoutParams(params);
    return view;
  }
}

或者


import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MyViewManager extends ViewGroupManager {

  @Override
  public String getName() {
    return "MyView";
  }

  @Override
  protected LinearLayout createViewInstance(ThemedReactContext reactContext) {
    LinearLayout view = new LinearLayout(reactContext);

    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    ((ViewGroup.MarginLayoutParams) params).leftMargin = 20;
    ((ViewGroup.MarginLayoutParams) params).rightMargin = 20;
    ((ViewGroup.MarginLayoutParams) params).topMargin = 20;
    ((ViewGroup.MarginLayoutParams) params).bottomMargin = 20;

    view.setLayoutParams(params);
    return view;
  }
}

这样就能成功设置外边距,并避免出现"Cannot get layout margins of multi-edge shorthands"错误。