我是flexbox的新手,无法在react-native中生成全宽按钮。我一直想把自己搞清楚一天,并且已经阅读了互联网上的每篇相关文章/帖子都无济于事。
我希望有两个TextInput
元素跨越屏幕的整个宽度,它们下方的按钮也跨越屏幕的整个宽度。 TextInput
元素是跨越屏幕的整个宽度,但这似乎是我正在运行的Android模拟器中的默认值。
这是我的代码:
var MyModule = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={styles.headline}>
<Text>Hello World</Text>
</View>
<View style={styles.inputsContainer}>
<TextInput style={[styles.input]} placeholder="Email" />
<TextInput
secureTextEntry={true}
style={[styles.input]}
placeholder="Password"
/>
<TouchableHighlight
style={styles.fullWidthButton}
onPress={this.buttonPressed}
>
<Text>Submit</Text>
</TouchableHighlight>
</View>
</View>
);
},
buttonPressed: function() {
console.log("button was pressed!");
}
});
var paddingLeft = 15;
var styles = StyleSheet.create({
inputsContainer: {
// Intentionally blank because I've tried everything & I'm clueless
},
fullWidthButton: {
// Intentionally blank because I've tried everything & I'm clueless
},
input: {
paddingLeft: paddingLeft,
height: 40,
borderColor: "black",
backgroundColor: "white"
},
container: {
flex: 1,
backgroundColor: "#f0f0f0",
alignItems: "stretch"
},
headline: {}
});
module.exports = Onboarding;
任何人都知道我需要做些什么才能让TouchableHighlight
跨越整个屏幕宽度?
答案 0 :(得分:50)
我来这里寻找同样的问题。经过进一步的实验,我发现最简单的方法是使用alignSelf: 'stretch'
。这迫使单个元素占据可用的全宽,例如
button: {
alignSelf: 'stretch'
}
Nader的答案当然有效,但这似乎是使用Flexbox的正确方法。
答案 1 :(得分:19)
您可以通过在TouchableHighlight的父元素上设置flex:1属性,然后将flexDirection:row属性指定给TouchableHighlight元素来实现此目的:
IOProc
我已经设置了一个完整的工作示例here。此外,完整代码如下。
https://rnplay.org/apps/J6fnqg
<View style={styles.inputsContainer}>
<TouchableHighlight style={styles.fullWidthButton} onPress={this.buttonPressed}>
<Text style={styles.fullWidthButtonText}>Submit</Text>
</TouchableHighlight>
</View>
inputsContainer: {
flex: 1
},
fullWidthButton: {
backgroundColor: 'blue',
height:70,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
fullWidthButtonText: {
fontSize:24,
color: 'white'
}
答案 2 :(得分:2)
现在有一个预定义的组件Button。即使您使用文本,也需要注意查看宽度:
<View style={[{width:"100%"}]}>
<Button
onPress={this.closeModal}
title="Close"
color="#841584"
style={[{borderRadius: 5,}]}
hardwareAccelerated
/>
</View>
答案 3 :(得分:2)
提供的解决方案对我不起作用。您必须将按钮包装在额外的 View
组件中:
<View style={{flexDirection: "row"}}>
<View style = {{flex: 1}}>
<Button title="Button 1" />
</View>
<View style = {{flex: 1}}>
<Button title="Button 2" />
</View>
</View>
或者将 Text
组件与 TouchableOpacity
一起使用:
<View style={{ flexDirection: "row"}}>
<TouchableOpacity style = {{width: "50%"}}>
<Text style={{textAlign:"center"}} > Button 1 </Text>
</TouchableOpacity>
<TouchableOpacity style = {{width: "50%"}}>
<Text style={{textAlign:"center"}} > Button 1 </Text>
</TouchableOpacity>
</View>
在此处尝试两种解决方案:https://snack.expo.io/wAENhUQrp
justifyContent: 'space-between'
不会为按钮使用所有可用空间button {alignSelf: 'stretch'}
不适用于 Button 组件答案 4 :(得分:1)
让我们使用下面的信息来帮助设置按钮的宽度和高度。在这里,您需要在视图布局中指定按钮的width和height参数。
<View style={[{ width: "90%", margin: 10, backgroundColor: "red" }]}>
<Button
onPress={this.buttonClickListener}
title="Button Three"
color="#90A4AE"
/>
</View>