【发布时间】:2017-10-14 20:55:30
【问题描述】:
我正在尝试在 React Native 中显示以下自定义原生 UI 组件
RectangleView.java
public class RectangleView extends View {
Paint mRectPaint;
public RectangleView(Context context) {
super(context);
init();
}
private void init() {
mRectPaint = new Paint();
mRectPaint.setColor(Color.BLUE);
mRectPaint.setStyle(Paint.Style.FILL);
}
public void setColor(String color) {
// mRectPaint.setColor(Color.parseColor(color));
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, 500, 500, mRectPaint);
}
}
这是我的 React 代码中的包装器
RectangleView.js
import React, { Component, PropTypes } from 'react';
import { requireNativeComponent, View, Text } from 'react-native';
const RCTRectangleView = requireNativeComponent('RCTRectangleView', RectangleView);
export default class RectangleView extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View>
<Text> Hello world </Text>
<RCTRectangleView {...this.props} />
</View>
);
}
}
RectangleView.propTypes = {
...View.propTypes,
color: PropTypes.string
};
我看到“Hello World”,但没有看到我希望看到的蓝色矩形。我的控制台中唯一的消息是
05-15 16:18:02.299 10937 10969 W ReactNativeJS:警告:View.propTypes 已被弃用,并将在未来版本的 ReactNative 中删除。请改用 ViewPropTypes。
05-15 16:18:02.316 10937 10969 I ReactNativeJS:使用 appParams 运行应用程序“CustomViewTest”:{"initialProps":{},"rootTag":1}。 DEV === true,开发级警告开启,性能优化关闭
我猜需要有一种方法来指定自定义视图的宽度和高度?无法在网上找到执行此类操作的具体示例。
【问题讨论】:
-
使用
import { requireNativeComponent, View, Text, ViewPropTypes } from 'react-native'; -
你有没有发现哪里出了问题?
标签: java android react-native react-native-android