首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法更改从react-native-paper导入的按钮的背景色

无法更改从react-native-paper导入的按钮的背景色
EN

Stack Overflow用户
提问于 2019-10-31 01:33:34
回答 2查看 2.1K关注 0票数 1

我还是个新手,在学习教程后才开始原生反应。我注意到android和iOS之间的按钮不一致,所以我想我应该试试react-native-paper库。

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

我该如何操作颜色呢?另外,有没有更好的库来保证android和iOS之间的按钮一致性?

谢谢

代码如下:

代码语言:javascript
复制
// 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添加了一个按钮来设置按钮内部文本的样式,以下是最终代码,将按钮设置为黑色背景和红色文本:

代码语言:javascript
复制
// 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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-31 18:04:01

看看react-native-paper Docs,看起来backgroundColor不是一个可以通过的有效道具。

要使用的属性只是color,所以它应该是:

color:{ "black" }

票数 0
EN

Stack Overflow用户

发布于 2019-10-31 21:22:05

我认为根据react-native-paper Docs,您可以在样式道具中添加自定义样式。在样式表中定义一些样式对象,并在按钮中指定该对象。

代码语言:javascript
复制
<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-elements buttons中,情况与此类似。(应该为自定义样式提供样式对象buttonStyle,而不是style。)还要确保您创建了一个样式表,并确保像这样导入该样式表

代码语言:javascript
复制
import { View, Text,StyleSheet } from 'react-native';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58630872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档