TouchableHighlight 和 TouchableOpacity 影响 Android 上的布局
TouchableHighlight and TouchableOpacity affect layout on Android
我注意到用 TouchableHighlight
或 TouchableOpacity
替换 TouchableWithoutFeedback
会导致布局不同。这是预期的吗?
示例:
<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
:
替换为TouchableOpacity
:
样式是:
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',
},
解决方法是不引入wrapper view。只需直接在 TouchableHighlight
或 TouchableOpacity
:
上设置样式
<TouchableOpacity onPress={this.onClick} style={styles.row_container}>
<Text style={styles.row_text}>
{'I count the number of taps: ' + this.state.clicks}
</Text>
</TouchableOpacity>
在 TouchableOpacsity 而不是 View 上设置样式
<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>
根据文档回答问题"is this expected,",是的。
https://facebook.github.io/react-native/docs/touchableopacity
"Opacity is controlled by wrapping the children in an Animated.View, which is added to the view hierarchy. Be aware that this can affect layout."
我注意到用 TouchableHighlight
或 TouchableOpacity
替换 TouchableWithoutFeedback
会导致布局不同。这是预期的吗?
示例:
<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
:
替换为TouchableOpacity
:
样式是:
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',
},
解决方法是不引入wrapper view。只需直接在 TouchableHighlight
或 TouchableOpacity
:
<TouchableOpacity onPress={this.onClick} style={styles.row_container}>
<Text style={styles.row_text}>
{'I count the number of taps: ' + this.state.clicks}
</Text>
</TouchableOpacity>
在 TouchableOpacsity 而不是 View 上设置样式
<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>
根据文档回答问题"is this expected,",是的。
https://facebook.github.io/react-native/docs/touchableopacity
"Opacity is controlled by wrapping the children in an Animated.View, which is added to the view hierarchy. Be aware that this can affect layout."