react native中用TouchableOpacity制作渐变背景色的按钮

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',
	},
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值