【问题标题】:React Native. Native Android component is not rendered反应原生。原生 Android 组件未呈现
【发布时间】:2022-04-29 23:11:53
【问题描述】:

我想从 Android 向 React Native 添加一个组件。 但它不渲染,也没有尺寸。 如何解决此问题并显示组件? 用于连接组件的代码类。 我使用了指南和其他人的示例。

更新: 有一个错误: REACT_CLASS = "RCTImageView"; 它仍然存在于其余的尝试中。 改回 CustomView - 没有帮助。

CustomViewManager.java:

public class CustomViewManager extends SimpleViewManager<TextView> {

public static final String REACT_CLASS = "CustomView";

@Override
public String getName() {
    return REACT_CLASS;
}

@Override
public TextView createViewInstance(ThemedReactContext context) {

    TextView tv = new TextView(context);
    tv.setText("Текст");
    tv.setTextColor(Color.BLACK);
    tv.setWidth(100);
    tv.setHeight(100);
    return tv;

  }
}

CustomViewPackage.java:

public class CustomViewPackage implements ReactPackage {

@Override
public List<ViewManager>
createViewManagers(ReactApplicationContext reactContext) {
    return Arrays.<ViewManager>asList(
            new CustomViewManager()
    );
}

@Override
public List<NativeModule>
createNativeModules(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}

}

MainApplication.java:

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new CustomViewPackage()
  );
}

CustomTextView.js:

import React from 'react';
import {
   requireNativeComponent
} from 'react-native';

module.exports = requireNativeComponent('CustomView');

App.js:

export default class App extends Component {
render() {
  return (
    <View 
      style={ styles.container }
    >
      <CustomView/>
    </View>
  );
}
}

【问题讨论】:

    标签: android react-native react-native-android


    【解决方案1】:

    需要设置弹性尺寸。

    在 CustomView 组件中,您必须传递 style = {{flex: 1}}

    【讨论】:

    • 考虑发表评论或在您的答案中包含更多详细信息
    【解决方案2】:

    请确保您编写了可接受的 CSS。不要使用 '12pt'、'12' 和任何未安装在我们的应用程序中的字体系列 CSS。如果您的 android 模拟器在 Expo 编辑器中出现故障,请再次检查所有组件 CSS。

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多