admin管理员组文章数量:1312782
I'm using react native to create a test App, There is no effect on a button when I do styling. can anyone tell me what I am doing wrong. For example, I am trying to put red color to a button but it is not working. What can I do to make it right?
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
Button,
ScrollView,
Dimensions,
PanResponder,
Animated,
View
} from 'react-native'
import { StackNavigator } from 'react-navigation'
class Home extends Component{
static navigationOptions = {
title:'Home'
};
ponentWillMount(){
this.animatedValue = new Animated.ValueXY();
this._value = {x:0 , y:0}
this.animatedValue.addListener((value) => this._value = value);
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderGrant: (e, gestureState) => {
this.animatedValue.setOffset({
x:this._value.x,
y:this._value.y,
})
this.animatedValue.setValue({x:0 , y:0})
},
onPanResponderMove:Animated.event([
null,{dx: this.animatedValue.x , dy:this.animatedValue.y}
]),
onPanResponderRelease: (e, gestureState) => {
},
})
}
render(){
var animatedStyle = {
transform:this.animatedValue.getTranslateTransform()
}
return(
<View style={styles.container}>
<Button
style={styles.button}
title="Login"
onPress={() => this.props.navigation.navigate("Login")} />
</View>
)
}
}
class Login extends Component{
static navigationOptions = {
title:'Login'
};
render(){
return(
<View>
<Text>home</Text>
</View>
)
}
}
const App = StackNavigator({
Home:{ screen: Home},
Login:{ screen:Login}
});
var styles = StyleSheet.create({
container: {
},
button:{
color:'red'
}
});
export default App
I'm using react native to create a test App, There is no effect on a button when I do styling. can anyone tell me what I am doing wrong. For example, I am trying to put red color to a button but it is not working. What can I do to make it right?
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
Button,
ScrollView,
Dimensions,
PanResponder,
Animated,
View
} from 'react-native'
import { StackNavigator } from 'react-navigation'
class Home extends Component{
static navigationOptions = {
title:'Home'
};
ponentWillMount(){
this.animatedValue = new Animated.ValueXY();
this._value = {x:0 , y:0}
this.animatedValue.addListener((value) => this._value = value);
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderGrant: (e, gestureState) => {
this.animatedValue.setOffset({
x:this._value.x,
y:this._value.y,
})
this.animatedValue.setValue({x:0 , y:0})
},
onPanResponderMove:Animated.event([
null,{dx: this.animatedValue.x , dy:this.animatedValue.y}
]),
onPanResponderRelease: (e, gestureState) => {
},
})
}
render(){
var animatedStyle = {
transform:this.animatedValue.getTranslateTransform()
}
return(
<View style={styles.container}>
<Button
style={styles.button}
title="Login"
onPress={() => this.props.navigation.navigate("Login")} />
</View>
)
}
}
class Login extends Component{
static navigationOptions = {
title:'Login'
};
render(){
return(
<View>
<Text>home</Text>
</View>
)
}
}
const App = StackNavigator({
Home:{ screen: Home},
Login:{ screen:Login}
});
var styles = StyleSheet.create({
container: {
},
button:{
color:'red'
}
});
export default App
Share
Improve this question
asked Jun 22, 2017 at 9:53
Vish_TSVish_TS
5402 gold badges7 silver badges19 bronze badges
4
- have you tried with inline style? – Jigar Shah Commented Jun 22, 2017 at 10:03
- Inline is working fine with the code. Issue is while importing it from external stylesheet. – Prabodh M Commented Jun 22, 2017 at 10:12
- well, other than that code looks okay, only button style is not working or all? – Jigar Shah Commented Jun 22, 2017 at 10:24
- yes, inline works :) – Vish_TS Commented Jun 22, 2017 at 11:18
2 Answers
Reset to default 6You cannot apply color to a button using stylesheet.
Checkout the link here
It has to be given inline. It is a property of button, its not like style attribute unlike tags like View
and Text
, where stylesheet applies.
If you give some style to your view container it will work, but not with button as its it not supported that way.
Solution :
<View style={styles.container}>
<Button
title="Login"
color="red"
onPress={() => this.props.navigation.navigate("Login")} />
</View>
Hope that helps!
If your button doesn't look right for your app, you can build your own button using TouchableOpacity or TouchableNativeFeedback. For inspiration, look at the source code for this button ponent. Or, take a look at the wide variety of button ponents built by the munity.
Look this:
<TouchableOpacity
style={[styles.buttonLargeContainer, styles.primaryButton]}
onPress={() => {}}>
<Text style={styles.buttonText}>SEND TO AUCTION?</Text>
</TouchableOpacity>
const styles = StyleSheet.create({
buttonLargeContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginRight: 10
},
primaryButton: {
backgroundColor: '#FF0017',
},
buttonText: {
color: 'white',
fontSize: 14,
fontWeight: 'bold'
}
})
本文标签: javascriptreact native button style not workingStack Overflow
版权声明:本文标题:javascript - react native button style not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741846346a2400801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论