【问题标题】:can't change background color of a button that's imported from react-native-paper无法更改从 react-native-paper 导入的按钮的背景颜色
【发布时间】:2020-02-26 01:50:55
【问题描述】:

我是新手,可以按照教程做出本机反应。我注意到 android 和 iOS 之间的按钮没有一致性,所以我想我会尝试 react-native-paper 库。

但是,从 react-native-paper 导入按钮后,我在更改按钮颜色时遇到了问题。颜色是恒定的颜色,如提供的图像中how the color looks

如何控制颜色?还有更好的库可以用于 android 和 iOS 之间的按钮一致性吗?

谢谢

这里是代码:

// import stuff
import React from 'react';
import { View, Text, TextInput, TouchableOpacity } from 'react-native';
import {
  Provider as PaperProvider,
  DarkTheme,
  DefaultTheme,
  Button
} from 'react-native-paper';
// import { Button } from 'react-native-paper';

//create stuff

class App extends React.Component {

  state = {
    text: "",
    todo: []
  }
  addTodo = () => {
    var newTodo = this.state.text
    var arr = this.state.todo
    arr.push(newTodo)
    this.setState({ todo: arr, text: "" })
  }

  deleteTodo = (t) => {
    var arr = this.state.todo;
    var pos = arr.indexOf(t);
    arr.splice(pos, 1);
    this.setState({ todo: arr });


  }

  renderTodos = () => {
    return this.state.todo.map(t => {
      return (
        <TouchableOpacity key={t}>
          <Text
            style={styles.todo}
            onPress={() => { this.deleteTodo(t) }}
          >{t}</Text>
        </TouchableOpacity>
      )
    })
  } 
  render() {
    return (
      <PaperProvider>
      <View style={styles.wholeStyle}>
        <View style={styles.viewStyle}>
          <Text style={styles.header}>Notes App</Text>
          <TextInput
            style={styles.inputStyle}
            onChangeText={(text) => this.setState({ text })}
            value={this.state.text}
          />
          <Button 
          onPress={this.addTodo}
          mode='contained'
          backgroundColor='black'
          >Todo</Button>
          {this.renderTodos()}
        </View>
      </View>
      </PaperProvider>
    )
  }
}

const styles = {
  wholeStyle: {
    flex: 1,
    backgroundColor: '#0288D1'
    // backgroundColor: 'red'
  },
  viewStyle: {
    alignItems: 'center',
    justifyContent: 'center',
    margin: 10,
    marginTop: 30,

  },
  inputStyle: {
    alignSelf: 'stretch',
    height: 40,
    borderColor: "white",
    borderWidth: 1

  },
  header: {
    fontSize: 40,
    color: 'white',
    fontWeight: 'bold'

  },
  todo: {
    fontSize: 18,
    color: 'white'
  }


}
//export stuff

export default App;

from docs, labelStyle 更新:感谢您的反馈,我设法更正了我的代码,使用属性 labelStyle 添加了一个按钮来设置按钮内的文本样式,这是最终代码,将按钮设置为黑色背景和红色文本:

// import stuff
import React from 'react';
import { View, Text, TextInput, TouchableOpacity } from 'react-native';
import {
  Provider as PaperProvider,
  DarkTheme,
  DefaultTheme,
  Button
} from 'react-native-paper';
// import { Button } from 'react-native-paper';

//create stuff

class App extends React.Component {

  state = {
    text: "",
    todo: []
  }
  addTodo = () => {
    var newTodo = this.state.text
    var arr = this.state.todo
    arr.push(newTodo)
    this.setState({ todo: arr, text: "" })
  }

  deleteTodo = (t) => {
    var arr = this.state.todo;
    var pos = arr.indexOf(t);
    arr.splice(pos, 1);
    this.setState({ todo: arr });


  }

  renderTodos = () => {
    return this.state.todo.map(t => {
      return (
        <TouchableOpacity key={t}>
          <Text
            style={styles.todo}
            onPress={() => { this.deleteTodo(t) }}
          >{t}</Text>
        </TouchableOpacity>
      )
    })
  }
  render() {
    return (
      <PaperProvider>
        <View style={styles.wholeStyle}>
          <View style={styles.viewStyle}>
            <Text style={styles.header}>Notes App</Text>
            <TextInput
              style={styles.inputStyle}
              onChangeText={(text) => this.setState({ text })}
              value={this.state.text}
            />
            <Button
              onPress={this.addTodo}
              mode='contained'
              color='black'
              labelStyle={styles.button}
            >Todo</Button>
            {this.renderTodos()}
          </View>
        </View>
      </PaperProvider>
    )
  }
}

const styles = {
  wholeStyle: {
    flex: 1,
    backgroundColor: '#0288D1'
    // backgroundColor: 'red'
  },
  viewStyle: {
    alignItems: 'center',
    justifyContent: 'center',
    margin: 10,
    marginTop: 30,

  },
  inputStyle: {
    alignSelf: 'stretch',
    height: 40,
    borderColor: "white",
    borderWidth: 1

  },
  header: {
    fontSize: 40,
    color: 'white',
    fontWeight: 'bold'

  },
  todo: {
    fontSize: 18,
    color: 'white'
  },
  button: {
    color: 'red'
  },

}
//export stuff

export default App;

【问题讨论】:

    标签: android ios react-native button background-color


    【解决方案1】:

    查看react-native-paper Docs 看起来backgroundColor 不是有效的传递道具。

    要使用的道具只是color,所以它是:

    color:{ "black" }

    【讨论】:

    • 谢谢,是的,我注意到它没有效果,所以我想知道,它是否应该始终具有静态背景颜色并且没有办法改变它?它是深蓝色的,不适合许多主题..
    • 你试过用颜色代替背景色吗?
    • 刚刚尝试过,它奏效了。 color 属性实际上设置了按钮的背景颜色。但是,现在我想知道实际上是什么改变了文本颜色
    • 将您的答案标记为正确答案,因为它最接近完整答案。我错过的是可以设置文本样式的属性 colorLabel。现在我在 iOS 和 android 上都有一致的按钮!感谢您的帮助
    • 不知道您也想更改标签颜色,react-native-paper 样式确实有限,因为您无法完全控制您的组件。大部分事情都必须通过主题来完成:callstack.github.io/react-native-paper/theming.html
    【解决方案2】:

    我认为根据react-native-paperDocs,可以在style prop中添加自定义样式。在样式表中定义一些样式对象并在按钮中指定该对象。

    <Button 
     onPress={this.addTodo}
     mode='contained'
     style={styles.mybuttonstyles}>Todo
    </Button>
    
    //In Your Styles Section, 
    
    const styles = StyleSheet.create({
     mybuttonstyles:{
      backgroundColor:'black'
      // all more styles you would like
     }
    

    我认为这应该可行。在react-native-elementsbuttons,类似。 (而不是样式,您应该为自定义样式提供样式对象 buttonStyle。)还要确保创建样式表并确保像

    一样导入该样式表
    import { View, Text,StyleSheet } from 'react-native';
    

    【讨论】:

    • 你的答案不正确,但它帮助我和 aiticat 找到了设置文本颜色和背景颜色的正确方法,你的权利我需要制作一个样式但是我需要使用根据文档的属性 labelStyle,我现在将相应地更新我的帖子,谢谢
    猜你喜欢
    • 2023-01-28
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2021-10-25
    • 2021-06-22
    • 2015-04-03
    相关资源
    最近更新 更多