TouchableHighlight和TouchableOpacity会影响Android上的布局

时间:2015-10-02 17:22:42

标签: react-native

我注意到用TouchableWithoutFeedbackTouchableHighlight替换TouchableOpacity会导致布局不同。这是预期的吗?

示例:

<TouchableWithoutFeedback onPress={this.onClick}>
  <View style={styles.row_container}>
    <Text style={styles.row_text}>
      {'I count the number of taps: ' + this.state.clicks}
    </Text>
  </View>
</TouchableWithoutFeedback>

使用TouchableWithoutFeedback

TouchableWithoutFeedback

将其替换为TouchableOpacity

TouchableHighlight

样式是:

row_container: {
  backgroundColor: 'rgba(0, 0, 0, 0.1)',
  flex: 1,
  height: 100,
  borderColor: '#333333',
  borderWidth: 1,
  borderRadius: 5,
  padding: 5,
},
row_text: {
  flex: 1,
  fontSize: 18,
  alignSelf: 'center',
},

4 个答案:

答案 0 :(得分:11)

解决方案不是介绍包装器视图。只需直接在TouchableHighlightTouchableOpacity上设置样式:

<TouchableOpacity onPress={this.onClick} style={styles.row_container}>
  <Text style={styles.row_text}>
    {'I count the number of taps: ' + this.state.clicks}
  </Text>
</TouchableOpacity>

答案 1 :(得分:1)

根据文档,要回答“这是预期的”问题吗?

https://facebook.github.io/react-native/docs/touchableopacity

“不透明度是通过将子级包装在Animated.View中控制的,该动画已添加到视图层次结构中。请注意,这会影响布局。”

答案 2 :(得分:0)

在TouchableOpacsity而不是View

上的

样式

答案 3 :(得分:0)

<TouchableOpacity style={{flex:1,justifyContent:'center',alignItems:'center',backgroundColor:'black',margin:2}}>
   <Image style={{width:30,height:30,marginBottom:3}} source={require('../images/movies_white.png')} />
   <Text style={{color:'#fff'}}>Lịch Sử</Text>
</TouchableOpacity>

而不是

<View style={{flex:1,justifyContent:'center',alignItems:'center',backgroundColor:'black',margin:2}}>
   <Image style={{width:30,height:30,marginBottom:3}} source={require('../images/movies_white.png')} />
    <Text style={{color:'#fff'}}>Lịch Sử</Text>
</View>
相关问题