简体   繁体   English

如何在react-native中为带有图标的按钮建模

[英]How to model a button with icons in react-native

I'm using react-native-icons package to include icons with buttons.我正在使用react-native-icons包来包含带有按钮的图标。 They have a sample code listed in example folder .他们在示例文件夹中列出了示例代码。 I'm trying to achieve onPress on View but turns out react-native doesn't have onPress function for <View> component.我正在尝试在 View 上实现onPress ,但结果是 react-native 没有<View>组件的onPress功能。

I tried using <TouchableHighlight> but it can only have single child element in it not two like <Icon> and <Text> inside.我尝试使用<TouchableHighlight>但它只能有一个子元素,而不是像<Icon><Text>这样的两个子元素。

I also tried using <Text> with <Icon> and <Text> inside but <Text> can only have <Text> elements inside.我还尝试将<Text><Icon><Text> ,但<Text>只能在其中包含<Text>元素。

Has anyone have any luck achieving similar functionality ?有没有人有运气实现类似的功能?

带图标的示例按钮

<View onPress={this.onBooking} 
  style={styles.button}>
  <Icon
    name='fontawesome|facebook-square'
    size={25}
    color='#3b5998'
    style={{height:25,width:25}}/>
  <Text style={styles.buttonText}>Sign In with Facebook</Text>
</View>

If you are using react-native-icons module, you can try wrap both your icon and text in a view, then use TouchableHighlight on top of it.如果您使用的是react-native-icons模块,您可以尝试将图标和文本都包裹在一个视图中,然后在它上面使用 TouchableHighlight。 Something like:就像是:

<TouchableHighlight onPress={()=>{}}>
     <View>
         <Icon ... />
         <Text ... />
     </View>
 </TouchableHighlight>

But it will be very easy if you use a relative new module react-native-vector-icons .但是,如果您使用相对较新的模块react-native-vector-icons ,这将非常容易。 You can do like:你可以这样做:

<Icon name="facebook" style={styles.icon}>
   <Text style={styles.text}>Login with Facebook</Text>
</Icon>

I managed to do it like this.我设法这样做了。 I wonder if there is a better way.我想知道是否有更好的方法。

var styles = StyleSheet.create({
  btnClickContain: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'stretch',
    alignSelf: 'stretch',
    backgroundColor: '#009D6E',
    borderRadius: 5,
    padding: 5,
    marginTop: 5,
    marginBottom: 5,
  },
  btnContainer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'stretch',
    alignSelf: 'stretch',
    borderRadius: 10,
  },
  btnIcon: {
    height: 25,
    width: 25,
  },
  btnText: {
    fontSize: 18,
    color: '#FAFAFA',
    marginLeft: 10,
    marginTop: 2,
  }
});

<TouchableHighlight
  onPress={this.onBooking} style={styles.btnClickContain}
  underlayColor='#042417'>
  <View
    style={styles.btnContainer}>
    <Icon
      name='fontawesome|facebook-square'
      size={25}
      color='#042'
      style={styles.btnIcon}/>
    <Text style={styles.btnText}>Sign In with Facebook</Text>
  </View>
</TouchableHighlight>

Expo has a a Button component that does what you want, styling and all: Expo 有一个Button组件,可以执行您想要的操作、样式和所有操作:

<FontAwesome.Button name="facebook" backgroundColor="#3b5998" onPress={...}>
  Sign in with Facebook
</FontAwesome.Button>
<FontAwesome.Button name="twitter" backgroundColor="#1da1f2" onPress={...}>
  Sign in with Twitter
</FontAwesome.Button>

What it looks like running in the iOS simulator:在 iOS 模拟器中运行的样子:

在此处输入图片说明

If you're not using Expo , study the source and create a similar component.如果您不使用Expo ,请研究源代码并创建一个类似的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM