React-native TouchableOpacity按钮不考虑边框/边框半径

hvvq6cgz  于 2023-02-25  发布在  React
关注(0)|答案(4)|浏览(139)

我创建了一个通用的按钮,我希望它有圆边而不是正方形。我的按钮组件如下:

const Button = ({ onPress, children }) => {
  const { buttonStyle, textStyle } = styles;

  return (
    <TouchableOpacity onPress={onPress} style={buttonStyle}>
      <Text style={textStyle}>
          {children}
      </Text>
    </TouchableOpacity>
  );
};

const styles = {
    textStyle: {
        alignSelf: 'center',
        color: colors.primaryTeal,
        fontSize: 16,
        fontWeight: '600',
        paddingTop: 10,
        paddingBottom: 10,
    },
    buttonStyle: {
        flex: 1,
        backgroundColor: colors.whiteText,
        marginLeft: 5,
        marginRight: 5,
        borderRadius: 50
    }
};

然而,它仍然是正方形的,并且根本不响应borderRadius
它是这样调用的:

<Button onPress={this.onButtonPress.bind(this)}>
    Log in
</Button>

我如何使它尊重borderRadius并得到圆边?
显示它的登录表单(Render)

render() {
    return (
        <Card>
            <CardSection>
                <Input
                    placeholder="user@gmail.com"
                    label="Email"
                    value={this.state.email}
                    onChangeText={email => this.setState({ email })}
                />
            </CardSection>
            <CardSection>
                <Input
                    secureTextEntry
                    placeholder="password"
                    label="Password"
                    value={this.state.password}
                    onChangeText={password => this.setState({ password })}
                />
            </CardSection>
            <View style={styles.btnWrapper}>
            <CardSection>
                {this.state.loading ? 
                    <Spinner size="small" /> : 
                    <Button onPress={this.onButtonPress.bind(this)}>
                        Log in
                    </Button>}
            </CardSection>
            </View>
            <Text style={styles.errorTextStyle} disabled={this.state.error !== null}>
                {this.state.error}
            </Text>
        </Card>

CardSection组件:

const CardSection = (props) => {
  return (
    <View style={styles.containerStyle}>
      {props.children}
    </View>
  );
};

const styles = {
  containerStyle: {
    borderBottomWidth: 1,
    padding: 5,
    backgroundColor: '#fff',
    justifyContent: 'flex-start',
    flexDirection: 'row',
    borderColor: '#ddd',
    position: 'relative'
  }
};
mrwjdhj3

mrwjdhj31#

工作得很好,只是要确保使用react native的StyleSheet.create来获取缓存样式,也许你的按钮容器视图背景是白色的,你看不到圆角。
这是我的工作snack
我用的代码片段,但指的是小吃,因为你会看到它的行动。

import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';

const Button = ({ onPress, children }) => {

  return (
    <TouchableOpacity onPress={onPress} style={styles.buttonStyle}>
      <Text style={styles.textStyle}>
          {children}
      </Text>
    </TouchableOpacity>
  );
};

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Button>
          Log in
        </Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex:1,
    backgroundColor: 'black',
  },
  textStyle: {
        alignSelf: 'center',
        color: 'teal',
        fontSize: 16,
        fontWeight: '600',
        paddingTop: 10,
        paddingBottom: 10,
    },
    buttonStyle: {
        flex: 1,
        backgroundColor: 'white',
        marginLeft: 5,
        marginRight: 5,
        borderRadius: 50
    },
});
ego6inou

ego6inou2#

必须将样式overflow: hidden添加到TouchableOpacity

dfddblmv

dfddblmv3#

尝试在buttonStyle上添加属性borderWidth和borderColor,如下所示:

buttonStyle: {
    backgroundColor: colors.whiteText,
    marginLeft: 5,
    marginRight: 5,
    borderRadius: 50,
    borderWidth: 2,
    borderColor: "#3b5998"
}

完整示例:

<TouchableOpacity
        onPress={() => handleSubmit()}
        disabled={!isValid}
        style={{
          alignSelf: "center",
          marginBottom: 30,
          overflow: 'hidden',
          borderColor: "#3b5998",
          borderWidth: 2,
          borderRadius: 100,
        }}
      >
        <View
          style={{               
            flexDirection: "row",
            alignSelf: "center",
            paddingLeft: 15,
            paddingRight: 15,
            minHeight: 40,
            alignItems: 'center',                
            
          }}
        >
          <Text style={{ fontSize: 20, fontWeight: "bold", color: "#3b5998" }}>
            SEND
          </Text>
        </View>
      </TouchableOpacity>
ejk8hzay

ejk8hzay4#

我想你可能在找containerStyle prop

<TouchableOpacity containerStyle={ViewStyleProps}>

相关问题