React Native实现菜单按钮镂空透明效果的技术方案咨询
React Native实现带嵌入按钮的凹口导航栏
下面给你两种可行的实现方案,对应你需要的效果:
方案一:SVG背景+绝对定位按钮(推荐,精准匹配设计)
这种方式能完美还原设计师的凹口形状,需要依赖react-native-svg库。
步骤1:安装依赖
npm install react-native-svg # 或者 yarn add react-native-svg
步骤2:实现组件
import React from 'react'; import { View, TouchableOpacity, StyleSheet } from 'react-native'; import { Svg, Path } from 'react-native-svg'; const BottomNavbar = () => { return ( <View style={styles.navWrapper}> {/* 带凹口的SVG背景 */} <Svg style={styles.navBg} viewBox="0 0 375 80"> {/* Path路径可根据设计稿调整坐标,这里是中间圆形凹口的示例 */} <Path d="M0,0 H375 V60 H187.5 C187.5,60 172.5,60 172.5,45 C172.5,30 187.5,30 187.5,30 C202.5,30 217.5,30 217.5,45 C217.5,60 187.5,60 187.5,60 H0 V0 Z" fill="#ffffff" stroke="#e0e0e0" /> </Svg> {/* 嵌入凹口的中间按钮 */} <TouchableOpacity style={styles.centerBtn}> {/* 这里替换成你的按钮图标,比如Ionicons/FontAwesome组件 */} </TouchableOpacity> {/* 左右导航项 */} <View style={styles.navItems}> <TouchableOpacity style={styles.navItem}> {/* 左侧导航图标 */} </TouchableOpacity> <TouchableOpacity style={styles.navItem}> {/* 右侧导航图标 */} </TouchableOpacity> </View> </View> ); }; const styles = StyleSheet.create({ navWrapper: { position: 'absolute', bottom: 0, left: 0, right: 0, height: 80, alignItems: 'center', }, navBg: { position: 'absolute', width: '100%', height: '100%', }, centerBtn: { width: 70, height: 70, borderRadius: 35, backgroundColor: '#2563eb', justifyContent: 'center', alignItems: 'center', bottom: 20, // 向上偏移嵌入凹口 shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.3, shadowRadius: 4, elevation: 5, // Android阴影 zIndex: 10, // 确保按钮在背景上方 }, navItems: { position: 'absolute', bottom: 10, left: 0, right: 0, flexDirection: 'row', justifyContent: 'space-around', paddingHorizontal: 40, }, navItem: { width: 50, height: 50, justifyContent: 'center', alignItems: 'center', }, }); export default BottomNavbar;
说明
- SVG的
viewBox要和导航栏的实际尺寸对应,Path的坐标可以用设计工具(比如Figma)导出,确保完全匹配设计师的凹口形状。 - 如果需要更灵活的裁剪,也可以用
<ClipPath>标签来实现背景的凹口效果。
方案二:纯View组合(无额外依赖,适合简单凹口)
如果凹口是标准的弧形,也可以不用SVG,直接用View的borderRadius拼接:
import React from 'react'; import { View, TouchableOpacity, StyleSheet } from 'react-native'; const BottomNavbar = () => { return ( <View style={styles.navWrapper}> {/* 左侧导航背景 */} <View style={styles.leftBg} /> {/* 右侧导航背景 */} <View style={styles.rightBg} /> {/* 中间嵌入按钮 */} <TouchableOpacity style={styles.centerBtn}> {/* 按钮图标 */} </TouchableOpacity> {/* 左右导航项 */} <TouchableOpacity style={styles.leftItem}> {/* 左侧图标 */} </TouchableOpacity> <TouchableOpacity style={styles.rightItem}> {/* 右侧图标 */} </TouchableOpacity> </View> ); }; const styles = StyleSheet.create({ navWrapper: { position: 'absolute', bottom: 0, left: 0, right: 0, height: 80, flexDirection: 'row', alignItems: 'flex-end', }, leftBg: { flex: 1, height: 60, backgroundColor: '#fff', borderTopRightRadius: 25, borderWidth: 1, borderColor: '#e0e0e0', borderLeft: 0, borderBottom: 0, }, rightBg: { flex: 1, height: 60, backgroundColor: '#fff', borderTopLeftRadius: 25, borderWidth: 1, borderColor: '#e0e0e0', borderRight: 0, borderBottom: 0, }, centerBtn: { position: 'absolute', bottom: 20, width: 70, height: 70, borderRadius: 35, backgroundColor: '#2563eb', justifyContent: 'center', alignItems: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.3, shadowRadius: 4, elevation: 5, zIndex: 10, }, leftItem: { position: 'absolute', left: 40, bottom: 10, width: 50, height: 50, justifyContent: 'center', alignItems: 'center', }, rightItem: { position: 'absolute', right: 40, bottom: 10, width: 50, height: 50, justifyContent: 'center', alignItems: 'center', }, }); export default BottomNavbar;
说明
- 这种方法通过左右两个带圆角的View拼接出凹口,优点是不需要额外依赖,但凹口形状只能是标准圆弧,无法适配复杂的自定义形状。
内容的提问来源于stack exchange,提问作者Brad Herman
相关产品推荐
相关产品推荐

