react-native-elements,按钮边距不生效

时间:2019-05-08 00:54:04

标签: react-native react-native-elements

我遇到一个问题,在应用marginTop: 15属性后,按钮上方有一个白色阴影。

之所以会这样,是因为buttonStyle将样式应用于内部View,并且由于高程(阴影)应用于了外部View,因此您基本上在外部创建了“填充” View

我希望可以通过以下方式解决该问题,如下所示:

import React, { Component } from "react";
import { View, Text, ScrollView, Dimensions } from "react-native";
import { Button } from "react-native-elements";

const SCREEN_WIDTH = Dimensions.get("window").width;

class Slides extends Component {
  renderLastSlide(index) {
    if (index === this.props.data.length - 1) {
      return (
        <Button
          title="Onwards!"
          raised
          buttonStyle={styles.buttonStyle}
          containerViewStyle={{ marginTop: 15 }}
          onPress={this.props.onComplete}
        />
      );
    }
  }

  renderSlides() {
    return this.props.data.map((slide, index) => {
      return (
        <View
          key={slide.text}
          style={[styles.slideStyle, { backgroundColor: slide.color }]}
        >
          <Text style={styles.textStyles}>{slide.text}</Text>
          {this.renderLastSlide(index)}
        </View>
      );
    });
  }

  render() {
    return (
      <ScrollView horizontal style={{ flex: 1 }} pagingEnabled>
        {this.renderSlides()}
      </ScrollView>
    );
  }
}

const styles = {
  slideStyle: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    width: SCREEN_WIDTH
  },
  textStyles: {
    fontSize: 30,
    color: "white",
    textAlign: "center"
  },
  buttonStyle: {
    backgroundColor: "#0288D1"
  }
};

export default Slides;

但是marginTop: 15现在对按钮无效。我不确定在这里还能做什么。

2 个答案:

答案 0 :(得分:0)

您可以尝试View

<View style={{marginTop: 15}}}>
  <Button
          title="Onwards!"
          raised
          buttonStyle={styles.buttonStyle}
          onPress={this.props.onComplete}
        />
<View>

答案 1 :(得分:0)

不要使用“style”将样式应用到 React Native Elements 组件,而是使用 containerStyle

以下是对其指示的文档的引用:

https://reactnativeelements.com/docs/customization/

示例:

        // A React Native Elements button
        <Button
            containerStyle={styles.button} // apply styles calling "containerStyle"
            title="Sign up"
        />

       // On the StyleSheet:
       const styles = StyleSheet.create({
        
         button: {
           color: '#C830CC',
           margin: 10,
         },
    
       });