Bib*_*ibs 38 flexbox reactjs react-native
我是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;
Run Code Online (Sandbox Code Playgroud)
任何人都知道我需要做什么TouchableHighlight来跨越屏幕的整个宽度?
Fin*_*lay 55
我来这里寻找同样的问题.经过进一步的实验,我发现最简单的方法就是使用alignSelf: 'stretch'.这迫使单个元件占据可用的全宽度,例如
button: {
alignSelf: 'stretch'
}
Run Code Online (Sandbox Code Playgroud)
Nader的答案当然有效,但这似乎是使用Flexbox的正确方法.
Nad*_*bit 23
您可以通过在TouchableHighlight的父元素上设置flex:1属性,然后将flexDirection:row属性分配给TouchableHighlight元素来实现此目的:
<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'
}
Run Code Online (Sandbox Code Playgroud)
我已经建立了一个完整的工作示例在这里.此外,完整代码如下.
https://rnplay.org/apps/J6fnqg
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
} = React;
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 style={styles.fullWidthButtonText}>Submit</Text>
</TouchableHighlight>
</View>
</View>
},
buttonPressed: function() {
console.log('button was pressed!');
}
});
var paddingLeft = 15;
var styles = StyleSheet.create({
inputsContainer: {
flex: 1
},
fullWidthButton: {
backgroundColor: 'blue',
height:70,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
fullWidthButtonText: {
fontSize:24,
color: 'white'
},
input: {
paddingLeft: paddingLeft,
height: 40,
borderColor: 'black',
backgroundColor: 'white',
},
container: {
flex: 1,
backgroundColor: '#f0f0f0',
alignItems: 'stretch',
},
headline: {
}
});
AppRegistry.registerComponent('MyModule', () => MyModule);
Run Code Online (Sandbox Code Playgroud)
Chr*_*way 13
提供的解决方案对我不起作用。您必须将按钮包装在附加View组件中:
<View style={{flexDirection: "row"}}>
<View style = {{flex: 1}}>
<Button title="Button 1" />
</View>
<View style = {{flex: 1}}>
<Button title="Button 2" />
</View>
</View>
Run Code Online (Sandbox Code Playgroud)
或者将该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>
Run Code Online (Sandbox Code Playgroud)
在这里尝试这两种解决方案: https: //snack.expo.io/wAENhUQrp
justifyContent: 'space-between'没有使用按钮的所有可用空间button {alignSelf: 'stretch'}不适用于 Button 组件| 归档时间: |
|
| 查看次数: |
48601 次 |
| 最近记录: |