ReactNative 封装按钮

1. 首先创建一个ButtonView.js

编写button 并设置样式

  <View style = {styles.container}>
        <TouchableHighlight
            style={[styles.btnDefaultStyle,this.props.btnStyle,styles.center]}
            activeOpacity={0.5}
            underlayColor={this.props.underlayColor}
            onPress={this.props.onPress}>

            <Text style={[this.props.TextStyle,styles.textDefaultStyle]}>{this.props.btnName}</Text>

        </TouchableHighlight>
    </View>
const styles = StyleSheet.create({
   container: {
      flexDirection:'row',
      justifyContent: 'center',
      alignItems: 'center',
  },
  center:{
      justifyContent:'center',
      alignItems:'center',
  },
  btnDefaultStyle:{
      height:30,
      width:100,
      backgroundColor: '#ff8447',
      borderColor: '#ff8447',
      borderRadius: 5,
  },
  textDefaultStyle:{
     color:'#ffffff',
      fontSize:15,
  },
});

TouchableHighlight 中样式 styles.btnDefaultStyle 标示默认样式 this.props.btnStyle 标示对外抛出的属性接口可以从外面修改

下面属性可以不写
 /*个属性
  *  btnName 按钮名称
  * textStyle 文本样式
  * btnStyle 按钮样式
  * underlayColor 点击后颜色
  * */
// static propTypes = {
//     btnName: PropTypes.string,
//     textStyle: Text.propTypes.style,
//     btnStyle: TouchableHighlight.propTypes.style,
//     underlayColor: TouchableHighlight.propTypes.underlayColor,
// }

给按钮默认值

 static defaultProps = {
    btnName: 'Button',
    underlayColor: 'gray',
};
2.从外面调用组件
<View style = {styles.container}>
      <ButtonView
        btnName='点击一下'
        btnStyle = {{width:200,height:30,backgroundColor:'red'}}
        onPress = {this._renderContent}
       ></ButtonView>
 </View>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,079评论 0 29
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • (一) 木垒的冬天很奇怪,十月开始下雪,来年的五月才会冰雪消融。十月份第一次下雪的时候...
    狼狼郎阅读 448评论 2 2
  • 【感恩有你】20170916学习力践行记录D124 践行:晚上带宝宝出去玩路上我背诵《手指摇》-小风筝、学说话
    恩恩妈阅读 192评论 0 0