React Native 自定义圆角button的封装

前段时间一直在做react native开发,一直在忙,也没时间写东西,这次就打算认真地写点东西了。
感觉react native开发就是要先写组件(component),然后用组件去组装页面。组件写好了,后续开发就简单多了。不多说了,下面介绍下自定义圆角Button的封装。

'use strict';

import React, {
   Component,
   PropTypes,
  } from 'react';

import {
  StyleSheet,
  PixelRatio,
  Text,
  View,
  TouchableHighlight,
  Platform,
} from 'react-native';

class RadiusBtn extends Component {

  static propTypes = {
    btnName: PropTypes.string,
    textStyle: Text.propTypes.style,
    btnStyle: TouchableHighlight.propTypes.style,
    underlayColor:       TouchableHighlight.propTypes.underlayColor,
  };

  static defaultProps = {
    btnName: 'Button',
    underlayColor: '#4169e1',
  };


  render() {
    return (
      <View style = {{
                    flexDirection: 'row',
                    justifyContent: 'center',
                    alignItems: 'center',}}>
          <TouchableHighlight
              underlayColor={this.props.underlayColor}
              activeOpacity={0.5}
              style={[styles.center, styles.btnDefaultStyle, this.props.btnStyle]}
              onPress={this.props.onPress}>
              <Text style={[styles.textDefaultStyle, this.props.textStyle]}>{this.props.btnName}</Text>
          </TouchableHighlight>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  center: {
    justifyContent:'center',
    alignItems: 'center',
  },
  btnDefaultStyle: {
    width: 100,
    height: 20,
    backgroundColor: '#ff8447',
    borderColor: '#ff8447',
    borderRadius: 15,
    borderWidth: (Platform.OS==='ios' ? 1.0 : 1.5) / PixelRatio.get(),
  },
  textDefaultStyle: {
    fontSize: 16,
    color: '#ffffff',
  },
});

module.exports = RadiusBtn;

static propTypes = { }; 这个方法是对定义的属性的类型设置并检验传入的类型。

static defaultProps = {} ; 这个是设置一些组件的默认属性。

其他没什么说的了,比较简单,用法如下:

<RadiusButton
                btnName= 'btnName'
                textStyle= {{
                            fontSize: 16,
                            color: '#ffffff',
                          }}
                btnStyle= {{
                            width: 300,
                            height: 44,
                            borderRadius: 25,
                          }}
                underlayColor= '#4169e1'
                onPress={this._pressCreditClick} >
              </RadiusButton>

挺简单的吧!看完这个基本就可以做复杂控件了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native中的自定义控件使用与React相同的组件方式进行实现。以下是自定义控件的基本步骤: 1. 创建一个自定义控件组件:在你的项目中创建一个新的组件,该组件包含你自定义的UI元素。 2. 定义控件属性:你可以在组件的props中定义一些属性,这些属性可以用来设置控件的外观和行为。 3. 实现控件逻辑:在组件的render方法中实现控件的逻辑,包括控件的事件处理、状态管理等。 4. 导出控件:将你的自定义控件组件导出,以便其他组件可以使用它。 以下是一个简单的例子,演示如何创建自定义控件: ``` import React, { Component } from 'react'; import { Text, TouchableOpacity } from 'react-native'; class CustomButton extends Component { constructor(props) { super(props); this.state = { pressed: false, }; } handlePress = () => { this.setState({ pressed: true }); }; handleRelease = () => { this.setState({ pressed: false }); }; render() { const { title, disabled } = this.props; const { pressed } = this.state; const buttonStyle = [ styles.button, disabled && styles.disabled, pressed && styles.pressed, ]; return ( <TouchableOpacity style={buttonStyle} onPress={this.handlePress} onPressOut={this.handleRelease} activeOpacity={0.6} disabled={disabled} > <Text style={styles.text}>{title}</Text> </TouchableOpacity> ); } } const styles = { button: { backgroundColor: '#007aff', paddingVertical: 10, paddingHorizontal: 20, borderRadius: 5, }, disabled: { opacity: 0.5, }, pressed: { backgroundColor: '#0051a8', }, text: { color: '#fff', fontSize: 16, fontWeight: 'bold', textAlign: 'center', }, }; export default CustomButton; ``` 在上面的例子中,我们创建了一个CustomButton组件,它包含一个TouchableOpacity,以及一些属性和状态来控制按钮的外观和行为。在render方法中,我们使用了一些简单的样式来设置按钮的外观,以及一些事件处理来处理按钮的行为。最后,我们将CustomButton组件导出,以便其他组件可以使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值