javascript - 我正在尝试使用 React-Native 将按钮放置在屏幕底部,我真的很难理解如何通过 flex 支持正确地做到这一点
问题描述
我正在尝试使用 React-Native 构建一个应用程序,我需要在屏幕底部放置两个按钮。到目前为止,我写了这段代码:
render() {
const { subtitle } = this.state;
return (
<View style={styles.container}>
<ActionBar onBackAction={this.backActionHandler} isBackAvailable />
<View style={styles.contentContainer}>
<Text>{subtitle}</Text>
</View>
<View style={styles.buttonsContainer}>
<Button title="BTN #1" />
<Button title="BTN #2" />
</View>
</View>
);
}
使用这些样式:
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-between'
},
contentContainer: {
flex: 1,
flexGrow: 1,
backgroundColor: 'purple'
},
buttonsContainer: {
flex: 1,
justifyContent: 'flex-end',
flexDirection: 'row',
marginBottom: 40,
backgroundColor: 'red'
}
});
这是迄今为止的结果......
这就是我想要实现的目标:
谁能指出我的错误在哪里?如果这很重要,则上面的整个屏幕将嵌套在另一个仅具有flex: 1
属性的视图中。我试图理解这种弹性概念,但我无法做到这一点。
解决方案
只需将样式更改flex
为。当您同时放入and时,可用空间分布在这两个容器之间。但根据您的要求,应该占用所有可用空间。所以,设置为正确。要获得所需的结果集,或者您可以避免使用.0
buttonsContainer
flex: 1
buttonsContainer
contentContainer
contentContainer
contentContainer
flex: 1
flex: 0
buttonsContainer
flex
buttonsContainer
在此处阅读有关 flex的更多信息。希望这会有所帮助!
推荐阅读
- wso2-am - WSO2 AM - 为 api 下载客户端 sdk 时,商店生成无效的 zip
- javascript - Reactjs objectlist groupby 和 map over
- c# - Fleck websocket 实现问题。无法使用套接字。发送
- linux - 使用 awk 忽略双引号
- javascript - 输入模式正则表达式
- c# - 查找带参数的方法引用
- c# - 有条件地复制数据表行
- html - 使用通用 ngModel 以两种不同的形式共享数据 angular 5
- android - CALL_STATE_IDLE 在第一次拨出电话时被调用?
- json - REST API - JSON 格式一致性