react中制作渐变色背景需要用到组件LinearGradient,但如果在原生组件Button中套用LinearGradient,没有办法实现按钮背景渐变色,结果会是两个组件分开显示。所以,官方文档中说,要自定义按钮样式,请使用组件TouchableOpacity。于是尝试在此组件中套用LinearGradient,并把它本身(其实它就是一个按钮)的backgroundColor设置为透明,结果可以成功显示(不会像在Button中套用一样分开显示),具体代码如下:
<TouchableOpacity style={styles.button}>
<LinearGradient colors={['rgba(252,67,2,1)','rgba(252,149,2,1)']} start={{ x: 1, y: 0 }} end={{ x: 0, y: 0 }} style={styles.buttonbg}>
<Text style={styles.buttontext}>登錄</Text>
</LinearGradient>
</TouchableOpacity>
const styles = StyleSheet.create({
button: {
height: 50,
marginTop: 50,
borderRadius: 50,
backgroundColor: 'rgba(255,255,255,0)',
},
buttontext: {
textAlign: 'center',
fontSize: 18,
},
buttonbg :{
borderRadius: 50,
height: 50,
justifyContent: 'center',
textAlign:'center',
},
});