【发布时间】:2025-12-17 19:50:02
【问题描述】:
我希望创建一个反应原生的可滚动视图提要,其高度为容器的 80%。我的代码目前看起来像
const FeedItem = () => (
<View style={styles.feedItem}>
<Text>Hello</Text>
</View>
);
const Feed = () => (
<ScrollView style={styles.feed} contentContainerStyle={styles.feedContentContainer}>
<FeedItem />
<FeedItem />
<FeedItem />
</ScrollView>
);
const App = () => (
<View style={styles.app}>
<Feed />
</View>
);
const styles = StyleSheet.create({
app: {
flex: 1,
},
feed: {
height: '100%',
},
feedContentContainer: {
height: '80%',
},
feedItem: {
height: '100%',
},
});
但是,这会导致滚动视图不再滚动。根据我所读到的内容,这与 flex 有关,我已经尝试了很多方法来解决这个问题,连续 2 天无济于事。有谁知道在不打乱视图滚动的情况下设置高度的正确方法?
我在 FlatList 中也遇到了同样的问题,所以希望 ScrollView 的答案也适用于 FlatList。谢谢!
【问题讨论】:
-
谢谢@CR7,我看过那篇文章,甚至在styles.feed 中添加height: 100 或height: '100%' 或flex: 1 都没有成功。 :(
-
尝试在滚动视图顶部的 Feed 组件内添加一个带有 flex 1 的新
-
还是没有运气。 :( 即使 ScrollView 位于 flex: 1 View 中,在 styles.feed 中设置高度也无济于事
标签: react-native react-native-flatlist react-native-scrollview react-native-flexbox