如何更改反应原生按钮的背景颜色

Mam*_*aly 19 javascript button reactjs react-native

我试图改变我的按钮的背景颜色,但似乎没有任何工作,我尝试了凸起的属性,也许我错误地使用它.你们有没有想法?

 import React from 'react';
 import { StyleSheet, Text, View, Button, TouchableHighlight } from 'react-native';

export default class App extends React.Component {
state={
  name: "Mamadou"
};

myPress = () => {
  this.setState({
    name: "Coulibaly"
  });
};

  render() {
    return (
      <View style={styles.container}>

          <Button       
          title={this.state.name}
          color="red"
          onPress={this.myPress}
        />   

      </View>

    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },

});
Run Code Online (Sandbox Code Playgroud)

Har*_*ngh 30

使用此选项将背景颜色添加到iOS中的按钮:

风格:

  loginScreenButton:{
    marginRight:40,
    marginLeft:40,
   marginTop:10,
    paddingTop:10,
    paddingBottom:10,
    backgroundColor:'#1E6738',
    borderRadius:10,
    borderWidth: 1,
    borderColor: '#fff'
  },
  loginText:{
      color:'#fff',
      textAlign:'center',
      paddingLeft : 10,
      paddingRight : 10
  }
Run Code Online (Sandbox Code Playgroud)

按钮:

<TouchableOpacity
          style={styles.loginScreenButton}
          onPress={() => navigate('HomeScreen')}
          underlayColor='#fff'>
          <Text style={styles.loginText}>Login</Text>
 </TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于Android,它只适用于Button颜色属性:

<Button  onPress={onPressAction}  title="Login"  color="#1E6738"  accessibilityLabel="Learn more about this button" />
Run Code Online (Sandbox Code Playgroud)


bod*_*sog 8

我建议使用React Native Elements包,它允许通过buttonStyle属性插入样式.

款式:

const styles = StyleSheet.create({
   container: {
      flex: 1
   },
   button: {
      backgroundColor: '#00aeef',
      borderColor: 'red',
      borderWidth: 5,
      borderRadius: 15       
   }
})
Run Code Online (Sandbox Code Playgroud)

渲染()

render() {
   return (
      <View style={styles.container}>
          <Button
             buttonStyle={styles.button}
             title="Example"
             onPress={() => {}}/>
      </View>
   )
}
Run Code Online (Sandbox Code Playgroud)

效果: 效果按钮

Expo Snack:https://snack.expo.io/Bk3zI0u0G

  • 它在 ios 上不起作用,请您检查并建议我。 (2认同)

Med*_*das 5

仅当您为 Android 构建时,“颜色”属性才适用于背景。

除此之外,我个人发现管理自定义按钮更容易。那就是创建您自己的名为按钮的组件,并将其作为带有文本的视图。这样它更易于管理,您可以像 Button 一样轻松导入它。


Sou*_* De -3

您应该使用十六进制代码backgroundColor="#FF0000"而不是color="red". 另外请使用raised={true}其他不被覆盖的明智背景颜色。