React Native中沒有使用CSS來進行樣式定義,因為CSS的引入全局設置的缺點塑煎。
React Native實現(xiàn)了部分CSS沫换,利用JavaScript中的Object來定義樣式,增強模塊化最铁。
Inline Style
在element的style屬性中直接定義
<Text>
The quick <Text style={{fontStyle: "italic"}}>brown</Text> fox
jumped over the lazy <Text style={{fontWeight: "bold"}}>dog</Text>.
</Text>
用object 進行style
將style的定義通過object寫在render之外
var italic = {
fontStyle: 'italic'
};
var bold={
fontWeight: 'bold'
};
...
render() {
return ( <Text>
The quick <Text style={italic}>brown</Text> fox
jumped over the lazy <Text style={bold}>dog</Text>.
</Text>
); }
Stylesheet.Create
通過stylesheet.create
也可以創(chuàng)建object類型的style讯赏,但是有一些額外的好處,這種style是不可變的冷尉,會透明的將他們轉(zhuǎn)換為引用table的index待逞,放在代碼最后可以保證只會被初始化一次。
Style Concatenation
可以將多個style同時作用到component上网严,如果定義有重復识樱,則最右邊的優(yōu)先
var AccentButton = React.createClass({ render: function() {
return (
<Text style={[styles.button, styles.accentText]}>
{this.props.children} </Text>
); }
});
通常style的定義可以和component的定義放在不同的文件中:
Position
React Native通過flexbox和margin和padding來定義view的position。
Flex中分為容器和item,容器中有主軸和cross軸
容器上的屬性:
- flexDirection: 主軸的方向怜庸,默認為"row"
- justifyContent:item在主軸上的排列当犯,默認為"flex-start"
- align-items:item在cross軸上的對齊,默認為“stretch”
Item上的屬性:
- flex: flex-grow割疾,flex-shrink嚎卫,flex-basis的簡寫,默認值為
0 1 auto
宏榕。后兩個屬性可選拓诸。
也可以通過絕對位置來布局
container: {
position: 'absolute',
top: 30,
left: 0,
right: 0,
bottom: 0
}