第四章、ReactNative组件的封装
来源:互联网 发布:java开方运算式 编辑:程序博客网 时间:2024/06/04 20:12
ReactNative组件的封装
官网地址https://facebook.github.io/react-native/docs/native-components-android.html
封装原生组件的步骤
1.创建一个ViewManager的子类。
2.实现createViewInstance方法。
3.导出视图的属性设置器:使用@ReactProp(或@ReactPropGroup)注解。
4.把这个视图管理类注册到应用程序包的createViewManagers里。
5.实现JavaScript模块。
第一步、创建ViewManager
public class XNButtonManager extends SimpleViewManager<CommonButton> { @Override public String getName() { return "CommonButton"; //1.CommonButton为现有自定义控件,对应RN中注册组件的名称; } @Override protected CommonButton createViewInstance(ThemedReactContext reactContext) { return new CommonButton(reactContext);//2.需要实现该接口,返回自定义控件的实例对象; } @ReactProp(name = "type") //设置按钮类型 public void setType(CommonButton button, int type) { button.setButtonType(type); button.invalidate(); } @ReactProp(name = "radius") public void setRadius(CommonButton button, float radius) { button.setRadius(radius); button.invalidate(); } @ReactProp(name = "frameColor") public void setFrameColor(CommonButton button, String frameColor) { button.setButtonFrameColor(frameColor); button.invalidate(); } @ReactProp(name = "fillColor") public void setFillColor(CommonButton button, String fillColor) { button.setButtonFillColor(fillColor); button.invalidate(); } @ReactProp(name = "text") public void setText(CommonButton button,String text){ button.setText(text); button.setGravity(Gravity.CENTER); button.invalidate(); } @ReactProp(name = "textColor") public void setTextColor(CommonButton button,String textColor){ button.setTextColor(Color.parseColor(textColor)); button.invalidate(); } @ReactProp(name ="frameWidth") public void setFrameWidth(CommonButton button,int frameWidth){ button.setButtonFrameWidth(frameWidth); button.invalidate(); }}
第二步、实现createViewManagers接口方法
public class XNReactPackage implements ReactPackage { @Override public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { return Arrays.<ViewManager>asList( new XNButtonManager() //在这里添加自定义控件的Manager实例对象; ); }}
第三步、注册ReactNative组件
在RN项目根目录下新建CommonButton.js文件
'use strict'import PropTypes from 'prop-types';import { requireNativeComponent, View } from 'react-native';//定义组件的属性及类型var CommonButton = { name : "CommonButton", propTypes : { type : PropTypes.number, frameColor: PropTypes.string, fillColor : PropTypes.string, radius : PropTypes.number, text : PropTypes.string, frameWidth: PropTypes.number, textColor : PropTypes.string, ...View.propTypes }}//导出组件module.exports = requireNativeComponent("CommonButton",CommonButton);
第四步、在RN中使用导出的组件
import CommonButton from '../modlue/CommonButton';... ...export default class Welcome extends Component { constructor(props) { super(props); } render() { return ( <View style={styles.container}> <CommonButton type = { 1 }//1 fill,2: stroke fillColor="#ff0000" frameColor="#ff0000" frameWidth={ 2 } radius={ 15 } text = "自定义按钮" textColor="#FFFFFF" style = { styles.commonButton } /> <Button style={styles.button} onPress={() => this.transfer()} title="首页" /> </View> ); }const styles = StyleSheet.create({ commonButton : { width:150, height:50 }});AppRegistry.registerComponent('Welcome', () => Welcome);
阅读全文
0 0
- 第四章、ReactNative组件的封装
- ReactNative 组件封装---Modal
- ReactNative的组件架构设计
- ReactNative的组件架构设计
- ReactNative – 组件的生命周期
- 《React-Native系列》38、 ReactNative混合组件封装
- 《React-Native系列》38、 ReactNative混合组件封装
- ReactNative开发——封装原生UI组件
- ReactNative进阶之评分控件的封装
- ReactNative开发之DrawerLayoutAndroid组件的使用
- ReactNative的Navigator组件使用方式
- ReactNative的Navigator组件使用方式
- ReactNative的Navigator组件使用方式
- reactNative中React组件的生命周期
- ReactNative开发——组件的引用
- ReactNative开发——组件的生命周期
- ReactNative 如何获取组件的引用
- ReactNative一个自己撸的menu组件
- HW0
- 递归--汉诺塔
- Hopfield神经网络的通俗理解
- 网页实现鼠标拖拽div
- sql server 中止运行时间超过阈值的作业
- 第四章、ReactNative组件的封装
- VC与JavaScript交互(三) ———— JS调用C++
- 数据库连接池C3P0学习
- Android Studio报错之Error:Execution failed for task ':app:processDebugResources'. > java.io.IOException
- Spark Checkpointing 错误恢复
- iOS11下compiling IB documents for earlier than ios 7 is no longer supported
- 打造扛得住的MySQL数据库架构-第2章 什么影响了MySQL性能
- Android 获取应用版本号与版本名
- C# ActiveMQ并行消费心得