agm*_*eod 17 flexbox react-native
所以我在视图的顶部有一个水平滚动视图.ScrollView包含具有指定宽度的节点.然后我在ScrollView的底部有一个边框,就像你可以在这个屏幕上看到的那样:http://i.imgur.com/pOV1JFP.png
如您所见,顶部ScrollView的子节点未到达边框.但是,如果我将ScrollView更改为View with flexDirection: 'row',则子节点将填充高度.我尝试在scrollview上更改一些属性,例如:
padding:0上contentContainerStyleautomaticallyAdjustContentInsets={false}contentInsets直接改变值似乎没有人解决这个问题.
scrollview代码和样式:
var styles = StyleSheet.create({
nav: {
padding: 0,
marginTop: 30,
flex: 1,
borderBottomWidth: 1,
borderColor: '#000000'
}
});
<ScrollView
style={[{width: screen.width}, styles.nav]}
horizontal={true}
showsHorizontalScrollIndicator={true}
automaticallyAdjustContentInsets={false}>
{dayNavItems}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
子组件(组成dayNavItems)
const styles = StyleSheet.create({
container: {
paddingLeft: 15,
paddingRight: 15,
width: 50,
justifyContent: 'center'
},
odd: {
backgroundColor: '#ccc'
},
selected: {
backgroundColor: '#39b54a'
},
text: {
fontFamily: 'Optima'
}
});
class DayNavItem extends React.Component {
static propTypes = {
day: React.PropTypes.object.isRequired,
odd: React.PropTypes.bool,
selectDay: React.PropTypes.func.isRequired,
selected: React.PropTypes.bool
};
render() {
const d = new Date(this.props.day.created_at);
const viewStyles = [styles.container];
if (this.props.selected) {
viewStyles.push(styles.selected);
} else if (this.props.odd) {
viewStyles.push(styles.odd);
}
return (
<TouchableOpacity style={viewStyles} onPress={() => this.props.selectDay(this.props.day.uuid)}>
<View>
<Text style={styles.text}>{getMonth(d)}</Text>
<Text style={styles.text}>{d.getDate()}</Text>
</View>
</TouchableOpacity>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Rap*_*nga 25
添加即可解决问题contentContainerStyle={{flexGrow: 1}}。
<ScrollView contentContainerStyle={{flexGrow: 1}}>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
有一个叫财产contentContainerStyle的ScrollView。我只是解决了类似的问题,我设置flex: 1的ScrollView的styles,ScrollView的contentContainerStyle,和子View组件。
其他答案是正确的,但只是为了提供一个澄清的例子:
<ScrollView contentContainerStyle={{ flex: 1 }}>
{children}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我总是最终在我的所有项目中创建这个组件:
import * as React from 'react'
import { ScrollView, ScrollViewProps, StyleSheet } from 'react-native'
export function FullHeightScrollView(
props: {
children: React.ReactNode
} & Omit<ScrollViewProps, 'contentContainerStyle'>
) {
return (
<ScrollView contentContainerStyle={styles.grow} {...props}>
{props.children}
</ScrollView>
)
}
const styles = StyleSheet.create({
grow: { flexGrow: 1 },
})
Run Code Online (Sandbox Code Playgroud)
使用它代替 React Native 的ScrollView.
| 归档时间: |
|
| 查看次数: |
8352 次 |
| 最近记录: |