我正在创建一个简单的操作按钮(浮动按钮)
这是工作:
<View style={{
width: this.props.size,
height: this.props.size,
borderRadius: this.props.size / 2,
backgroundColor: '#ee6e73',
position: 'absolute',
bottom: 10,
right: 10,
flexDirection:'row'
}}>
<Text>
+
</Text>
</View>
这不是 :
<TouchableOpacity
onPress={()=>{
}} >
<View style={{
width: this.props.size,
height: this.props.size,
borderRadius: this.props.size / 2,
backgroundColor: '#ee6e73',
position: 'absolute',
bottom: 10,
right: 10,
flexDirection:'row'
}}>
<Text>
+
</Text>
</View>
</TouchableOpacity>
只需用 TouchableOpacity 包裹,然后我的按钮就不会显示,没有任何错误。
反应0.1.7,安卓
然后我尝试将样式从 View 移动到 TouchableOpacity,这是工作
<TouchableOpacity
onPress={()=>{
}}
style={{
width: this.props.size,
height: this.props.size,
position: 'absolute',
borderRadius: this.props.size / 2,
backgroundColor: '#ee6e73',
bottom: 10,
right: 10,
}}>
<Text>
+
</Text>
</TouchableOpacity>
谁能解释一下为什么?
React Native 文档说
[https://facebook.github.io/react-native/docs/touchableopacity.html][1]
使视图正确响应触摸的包装器。
这是在没有实际更改视图层次结构的情况下完成的,
一般来说,很容易添加到应用程序中,而不会产生奇怪的副作用。
这意味着我包装了我原来的视图,它会按我的预期工作,但事实并非如此。