【发布时间】:2018-10-27 11:51:52
【问题描述】:
我正在使用带有 Flexbox 的 react-native 路由器。我想展示 FlatList 最后采用全屏和粘性页脚。我尝试为 FlatList 指定 flexGrow: 1,为 View 页脚指定 flexShrink: 0,但它不起作用。我正在 Expo 中构建应用并在 iPhone 上查看结果。
import React, {Component} from 'react';
import {
Text,
View,
FlatList,
} from 'react-native'
import {Provider} from 'react-redux'
import {NativeRouter, Route, Link} from 'react-router-native'
export default class App extends Component {
render() {
return <NativeRouter>
<View style={{
display: "flex",
flexDirection: "column",
padding: 10,
}}>
<View style={{
display: "flex",
flexDirection: 'row',
justifyContent: 'space-around'
}}>
<Link
to="/lobby"
underlayColor='#f0f4f7'
style={{
flex: 8,
alignItems: 'center',
padding: 10,
}}>
<Text>Lobby</Text>
</Link>
<Link
to="/"
underlayColor='#f0f4f7'
style={{
flex: 2,
alignItems: 'center',
padding: 10,
}}>
<Text>Home</Text>
</Link>
</View>
<Route exact path="/" component={Home}/>
<Route path="/lobby" component={Lobby}/>
</View>
</NativeRouter>
}
}
const Home = () => (
<Text>
Home
</Text>
);
class Lobby extends Component {
render() {
return <View style={{
flexGrow: 1,
flexDirection: "column",
// justifyContent: "space-between"
}}>
<FlatList
style={{
flexGrow: 1,
}}
data={[
{key: 'Spring Game'},
{key: 'Summer Game'},
{key: 'Fall Game'},
{key: 'Winter Game'},
]}
renderItem={({item}) => <Text style={{
display: "flex",
flex: 1,
padding: 10,
fontSize: 18,
height: 44,
alignContent: "center"
}}>{item.key}</Text>}
/>
<View style={{
display: "flex",
flexDirection: "row",
backgroundColor: "#848441",
flexShrink: 0,
}}>
<Text style={{
flex: 6,
alignSelf: "flex-end"
}}>Footer</Text>
</View>
</View>
}
}
【问题讨论】:
-
你需要你的主容器是一个弹性元素并设置为高度的 100vh/%,然后你的列表/中间容器设置为 flex-grow 为 1(完成)并允许溢出自动所以它溢出并且不要向下推页脚。这是一个基本的 CSS 示例来说明 codepen.io/anon/pen/NMEPNx
-
指定高度就足够了。我为标题指定了 10% 的高度,使用滚动条查看 90% 并将页脚放在滚动条视图中。
-
Whatever ;) ,注意容器的高度旁边不要超过视口的高度页脚和标题可以是任何高度,main 将填充剩余空间,直到它需要显示滚动条(如果你想要标题也向上滚动,然后将其移动到 main 内)。固定值或灵活值是这里唯一的区别。最适合您需求的就是最好的使用。
标签: html css react-native flexbox