【发布时间】:2015-12-07 17:34:11
【问题描述】:
我正在创建一个简单的操作按钮(浮动按钮)
这是有效的:
<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 包裹,然后我的按钮就不会显示而不会出现任何错误。
React 0.1.7,Android
然后我尝试将样式从 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]
用于使视图正确响应触摸的包装器。 这是在不实际更改视图层次结构的情况下完成的, 并且通常很容易添加到应用中而不会产生奇怪的副作用。
这意味着我包装了我的原始视图,它会按预期工作,但事实并非如此。
【问题讨论】:
-
有什么方法可以发布 this.props.size 的值?另外,您使用位置是否有特定原因:绝对?谢谢。
-
是的,我使用 position: absolute 将按钮浮动在其他按钮的前面。 this.props.size 用于设置此按钮的大小。这是一个数字。例如
标签: android react-native floating-action-button