【发布时间】:2017-04-02 10:07:14
【问题描述】:
我有 3 个组件,我正在尝试设置它们的高度(页眉、正文、页脚),但是当它呈现时,每个组件只有它包含的一行文本的高度。
import React from 'react';
import { View, Text } from 'react-native';
import Header from './Header';
import Body from './Body';
import Footer from './Footer';
import * as globalStyles from '../styles/global';
const HomeScreen = () => (
<View style={{ flex: 1 }}>
<Header style={{ flex: 1 }} title={'MyTitle'}>
<Text>Component</Text>
</Header>
<Body style={{ flex: 3 }}>
<Text>my body</Text>
</Body>
<Footer style={{ flex: 1 }}>
<Text>my footer</Text>
</Footer>
</View>
);
export default HomeScreen;
应用程序
import React from 'react';
import { Provider } from 'react-redux';
import HomeScreen from './components/HomeScreen';
import createStore from './createStore';
const store = createStore();
export default () => (
<Provider store={store}>
<HomeScreen />
</Provider>
);
标题
import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';
const Header = ({ children, title }) => (
<View>
<Text style={globalStyles.COMMON_STYLES.text}>
{children}
{title}
{title}
</Text>
</View>
);
Header.propTypes = {
children: PropTypes.node
};
const styles = StyleSheet.create({
header: {
paddingTop: 20
}
});
export default Header;
页脚
import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';
const Footer = ({ children }) => (
<View>
<Text style={globalStyles.COMMON_STYLES.text}>
{children}
</Text>
</View>
);
Footer.propTypes = {
children: PropTypes.node.isRequired
};
const styles = StyleSheet.create({
header: {
marginTop: 20,
flex: 1,
}
});
export default Footer;
身体
import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';
const Body = ({ children, title }) => (
<View>
<Text style={globalStyles.COMMON_STYLES.text}>
{children}
{title}
{title}
</Text>
</View>
);
Body.propTypes = {
children: PropTypes.node
};
const styles = StyleSheet.create({
header: {
marginTop: 20,
flex: 1,
}
});
export default Body;
全局样式
import { StyleSheet } from 'react-native';
export const BG_COLOR = '#343336';
export const BAR_COLOR = '#4e4d52';
export const TEXT_COLOR = '#e5dbda';
export const HEADER_TEXT_COLOR = '#fff';
export const MUTED_COLOR = '#8e8786';
export const LINK_COLOR = '#48e9d9';
export const ACCENT_COLORS = ['#d31d65', '#751c53', '#c248c0', '#7d6e8b', '#bbc6f7'];
export const COMMON_STYLES = StyleSheet.create({
pageContainer: {
backgroundColor: BG_COLOR,
marginTop: 0,
paddingTop: 5,
paddingHorizontal: 10
},
text: {
color: TEXT_COLOR,
fontFamily: 'Helvetica Neue'
}
});
我的理解是 Body 应该是页眉或页脚高度的 3 倍。
反应原生:
react-native-cli: 2.0.1
react-native: 0.41.2
【问题讨论】:
-
仅此代码就可以按您的预期正常工作。尝试共享该屏幕的整个代码。顺便说一句,您使用的是哪个版本的 RN?
-
我添加了更多代码 - 尽管那里有更多样式 - 我看不出它会如何影响高度。
-
我要找的是你使用 HomeScreen 的地方,它是否被包裹在其他容器中,是否为那些包裹容器定义了 flex(如果有)?
-
感谢您的回复 - 我不这么认为。请参阅我的更新答案。
-
Flex 应该被赋予页眉、正文和页脚组件的根容器。 Header、Footer 和 Body 是自定义组件,样式 props 不适用于它们,您应用的样式只是作为 prop 传递给子组件。
标签: react-native flexbox