gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:21:38 24 4
gpt4 key购买 nike

我是按照教程进行原生 react 的新手。我注意到 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;

最佳答案

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

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

颜色:{“黑色”}

关于android - 无法更改从react-native-paper导入的按钮的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58630872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com