下面的代碼可以在this live example反應本機文字離開我的屏幕,拒絕包裝。該怎麼辦?
我有以下發現反應本土元素:
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<View style={styles.descriptionContainerVer}>
<View style={styles.descriptionContainerHor}>
<Text style={styles.descriptionText} numberOfLines={5} >
Here is a really long text that you can do nothing about, its gonna be long wether you like it or not, so be prepared for it to go off screen. Right? Right..!
</Text>
</View>
</View>
<View style={styles.descriptionContainerVer2}>
<View style={styles.descriptionContainerHor}>
<Text style={styles.descriptionText} numberOfLines={5} >Some other long text which you can still do nothing about.. Off the screen we go then.</Text>
</View>
</View>
</View>);
}
});
AppRegistry.registerComponent('SampleApp',() => SampleApp);
與以下樣式:
var styles = StyleSheet.create({
container:{
flex:1,
flexDirection:'column',
justifyContent: 'flex-start',
backgroundColor: 'grey'
},
descriptionContainerVer:{
flex:0.5, //height (according to its parent)
flexDirection: 'column', //its children will be in a row
alignItems: 'center',
backgroundColor: 'blue',
// alignSelf: 'center',
},
descriptionContainerVer2:{
flex:0.5, //height (according to its parent)
flexDirection: 'column', //its children will be in a row
alignItems: 'center',
backgroundColor: 'orange',
// alignSelf: 'center',
},
descriptionContainerHor:{
//width: 200, //I DON\'T want this line here, because I need to support many screen sizes
flex: 0.3, //width (according to its parent)
flexDirection: 'column', //its children will be in a column
alignItems: 'center', //align items according to this parent (like setting self align on each item)
justifyContent: 'center',
flexWrap: 'wrap'
},
descriptionText: {
backgroundColor: 'green',//Colors.transparentColor,
fontSize: 16,
color: 'white',
textAlign: 'center',
flexWrap: 'wrap'
}
});
這將導致以下畫面:
如何阻止文本脫離屏幕並將其限制在屏幕中間,寬度爲父級的80%。
我不認爲我應該用width
,因爲我會在許多不同的手機屏幕上運行這一點,我希望它是動態的,所以我想我應該完全依靠flexbox
。
(這是最初的原因,我的descriptionContainerHor
內有flex: 0.8
我想實現的是這樣的:!
謝謝
謝謝,這是一個很好的答案,但我已經試過了,出於某種原因,它並不總是有效(不知道爲什麼:S)。flexwrap在反應原生中有點片狀。 +1雖然爲此帶來了。 – SudoPlz
這應該被接受回答 –
^^^這是這裏的實際答案。接受這個作品! – gregblass