我有3個組件,我試圖設置它們的高度(標題,正文,頁腳),但是當它呈現時,每個組件只有它包含的一行文本的高度。React-native flex height
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'
}
});
我的理解是,身體應該是3×頁眉或頁腳的高度。
陣營母語:
react-native-cli: 2.0.1
react-native: 0.41.2
此代碼按照您的預期正常工作。嘗試分享該屏幕的完整代碼。順便問一下你使用的是哪個版本的RN? – Hariks
我已經添加了更多的代碼 - 儘管那裏有更多的樣式 - 我看不出它會如何幹擾高度。 – Wasteland
我在尋找的是使用HomeScreen的地方,它是否包裹在其他容器中,是否爲那些包裝容器定義了flex(如果有)? – Hariks