bar*_*eek 43 javascript reactjs react-native
我对这整个"没有CSS"的东西感到困惑,但我理解为什么它有益.我想要做的只是在屏幕中间放置一个按钮,但我不明白样式在React中是如何工作的.这是我的代码:
var tapSpeed = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Tap me as fast as you can!
        </Text>
        <View style={styles.button}>
        !
        </View>
      </View>
    );
  }
});
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFCCCC'
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10
  },
  button: {
    textAlign: 'center',
    color: '#ffffff',
    marginBottom: 7,
    border: 1px solid blue,
    borderRadius: 2px
  }
});
Yev*_*nov 34
更新:使用内置的Button组件.
推荐使用:
包装你查看到TouchableHighlight iOS和TouchableNativeFeedback为Android.
var {
  Platform,
  TouchableHighlight,
  TouchableNativeFeedback 
} = React;
var tapSpeed = React.createClass({
  buttonClicked: function() {
    console.log('button clicked');
  },
  render: function() {
    var TouchableElement = TouchableHighlight;
    if (Platform.OS === 'android') {
     TouchableElement = TouchableNativeFeedback;
    }
    return (
    <View style={styles.container}>
      <Text style={styles.welcome}>
        Tap me as fast as you can!
      </Text>
      <TouchableElement
        style={styles.button}
        onPress={this.buttonClicked.bind(this)}>
        <View>
          <Text style={styles.buttonText}>Button!</Text>
        </View>
      </TouchableElement>        
    </View>
    );
  }
});       
Ash*_*k R 26
您可以使用内置的react-native Button元素.
import React, { Component } from 'react';
import { StyleSheet, View, Button, Alert, AppRegistry } from 'react-native';
class MainApp extends Component {
_onPress() {
  Alert.alert('on Press!');
 }
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.buttonContainer}>
          <Button onPress={this._onPress} title="Hello" color="#FFFFFF" accessibilityLabel="Tap on Me"/>
        </View>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF'
  },
  buttonContainer: {
    backgroundColor: '#2E9298',
    borderRadius: 10,
    padding: 10,
    shadowColor: '#000000',
    shadowOffset: {
      width: 0,
      height: 3
    },
    shadowRadius: 10,
    shadowOpacity: 0.25
  }
})
AppRegistry.registerComponent('MainApp', () => MainApp);
在这里阅读更多.
ide*_*ide 25
该反应天然按钮包提供的样式等天然按钮的按钮.安装它npm install react-native-button并在组件中使用它,如下所示:
var Button = require('react-native-button');
var ExampleComponent = React.createClass({
  render() {
    return (
      <Button
        style={{borderWidth: 1, borderColor: 'blue'}}
        onPress={this._handlePress}>
        Press Me!
      </Button>
    );
  },
  _handlePress(event) {
    console.log('Pressed!');
  },
});
| 归档时间: | 
 | 
| 查看次数: | 79911 次 | 
| 最近记录: |