我是新来的人,在听完本教程之后会做出反应。我注意到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;
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为根据react-native-paper
Docs,您可以在样式道具中添加自定义样式。在样式表中定义一些样式对象,然后在按钮中分配该对象。
<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。)还要确保创建样式表,并确保像
import { View, Text,StyleSheet } from 'react-native';