Javascript 反应本机梯度按钮
我有一个问题,我的btn组件,我使用反应本机线性梯度库 btn.jsJavascript 反应本机梯度按钮,javascript,reactjs,button,react-native,Javascript,Reactjs,Button,React Native,我有一个问题,我的btn组件,我使用反应本机线性梯度库 btn.js import React,{ Component } from 'react'; import {View,Text,StyleSheet, TouchableOpacity} from 'react-native'; import {Button,Item} from 'native-base'; import LinearGradient from 'react-native-linear-gradient'; cla
import React,{ Component } from 'react';
import {View,Text,StyleSheet, TouchableOpacity} from 'react-native';
import {Button,Item} from 'native-base';
import LinearGradient from 'react-native-linear-gradient';
class GradientBtn extends Component {
constructor(props){
super(props);
}
render(){
return(
<LinearGradient colors={['#d7d7d7', '#fafafa', '#e8e8e8']}
style={[styles.btnContainer,{height:this.props.h,width:this.props.w}]}>
<Text style={styles.btn}>{this.props.name}</Text>
</LinearGradient>
);
}
}
var styles = StyleSheet.create({
btnContainer:{
backgroundColor:'#f0f0f0',
justifyContent:'center',
marginLeft:1
},
btn:{
textAlign:'center',
color:'#000',
fontWeight:'700',
fontSize:12
}
});
export default GradientBtn;
<View style={{flexDirection:'row',justifyContent:'space-between',marginTop:5}}>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
</View>
import React,{Component}来自'React';
从“react native”导入{视图、文本、样式表、TouchableOpacity};
从“本机基”导入{按钮,项};
从“反应本机线性渐变”导入LinearGradient;
类GradientBtn扩展组件{
建造师(道具){
超级(道具);
}
render(){
报税表(
{this.props.name}
);
}
}
var styles=StyleSheet.create({
BTN容器:{
背景颜色:“#f0”,
辩护内容:'中心',
边缘左侧:1
},
btn:{
textAlign:“中心”,
颜色:“#000”,
重量:'700',
字体大小:12
}
});
导出默认梯度BTN;
app.js
import React,{ Component } from 'react';
import {View,Text,StyleSheet, TouchableOpacity} from 'react-native';
import {Button,Item} from 'native-base';
import LinearGradient from 'react-native-linear-gradient';
class GradientBtn extends Component {
constructor(props){
super(props);
}
render(){
return(
<LinearGradient colors={['#d7d7d7', '#fafafa', '#e8e8e8']}
style={[styles.btnContainer,{height:this.props.h,width:this.props.w}]}>
<Text style={styles.btn}>{this.props.name}</Text>
</LinearGradient>
);
}
}
var styles = StyleSheet.create({
btnContainer:{
backgroundColor:'#f0f0f0',
justifyContent:'center',
marginLeft:1
},
btn:{
textAlign:'center',
color:'#000',
fontWeight:'700',
fontSize:12
}
});
export default GradientBtn;
<View style={{flexDirection:'row',justifyContent:'space-between',marginTop:5}}>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
<TouchableOpacity>
<GradientBtn h={35} w={'24.55%'} name="Open Corrective"/>
</TouchableOpacity>
</View>
当我从组件中删除TouchableOpacity标记时,视图将正常,但我希望在该btn上触摸不透明度当我放置此标记时,我的视图将压缩表示宽度,并且没有看到正确的btn。尝试以下操作-
const GradientBtn = ({ name }) => (
<LinearGradient colors={['#d7d7d7', '#fafafa', '#e8e8e8']} style={styles.gradient}>
<Text style={styles.btn}>{name}</Text>
</LinearGradient>
)
const styles = StyleSheet.create({
gradient: {
flex: 1
}
});
虽然没有尝试过,但我想它会起作用:)这个答案很晚了,但看起来你根本没有在
可触摸不透明度上使用flex:1
。或者,您可以将高度和宽度设置在TouchableOpacity
上
这是一个在React Native中轻松使用渐变按钮的方法。使用
或
多恩
const styles=StyleSheet.create({
线性梯度:{
弹性:1,
paddingLeft:15
paddingRight:15,
边界半径:5
},
按钮文字:{
尺码:18,
fontFamily:“Gill Sans”,
textAlign:'中心',
差额:10,
颜色:“#ffffff”,
背景色:“透明”,
},
});
您可能想尝试在TouchableOpacity
上使用flex:1
。此外,以下是我的组件库,供参考:
<LinearGradient colors={['#4c669f', '#3b5998', '#192f6a']}
style={styles.linearGradient}
start={{ y: 0.0, x: 0.0 }} end={{ y: 0.0, x: 1.0 }}>
<Text style={styles.buttonText}> Done </Text>
</LinearGradient>
const styles = StyleSheet.create({
linearGradient: {
flex: 1,
paddingLeft: 15
paddingRight: 15,
borderRadius: 5
},
buttonText: {
fontSize: 18,
fontFamily: 'Gill Sans',
textAlign: 'center',
margin: 10,
color: '#ffffff',
backgroundColor: 'transparent',
},
});