在html中所有的布局都依賴于css樣式中的style繼承(css中確切來說應該是不存在繼承關系的,而是通過樣式名稱直接引用),css擁有非常豐富的特性。
在react native中,樣式的配飾和html有非常多的相似,比如說在html中聲明背景顏色采用 background 或者 background-color,但是在react native中采用 backgroundColor, 所有的樣式聲明都是采用這種峰駝式風格
。
View(視圖)
react native中的View組件類似與html中的div,下面是一些針對View布局的一些測試代碼。一個組件類中只能存在一個一級View。
正確的代碼
import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';
class FlexDimensionsBasics extends Component {
render() {
return (
<View><Text>hello world!</Text></View>
);
}
};
AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
錯誤的代碼
import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';
class FlexDimensionsBasics extends Component {
render() {
return (
<View><Text>hello world!</Text></View>
<View><Text>hello world!</Text></View>
);
}
};
AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
multiView(多個視圖)
一個組件類中只能存在一個一級View,因此多個View只能包裹在一級View中。
import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';
class FlexDimensionsBasics extends Component {
render() {
return (
<View>
<View><Text>hello world!</Text></View>
<View><Text>hello world!</Text></View>
<View><Text>hello world!</Text></View>
</View>
);
}
};
AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
View Layout(視圖布局)
布局涉及到的就是 寬
、高
、多個同級元素保持同一行(inline)
、自適應寬
、內間距
、外間距
、橫向居中
、豎向居中
、向左居中
、向右劇中
、固定位置
、,另外react native還提供了一種==自適應高==。
所有布局的詳細參數,請參考react native官網提供的說明文檔。
-
寬和高 引用官方源碼
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FixedDimensionsBasics extends Component { render() { return ( <View> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} /> <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FixedDimensionsBasics);
寬和高 -
多個同級元素保持同一行(inline) 引用官網源碼
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FlexDirectionBasics extends Component { render() { return ( <View style={{flex: 1, flexDirection: 'row'}}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} /> <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FlexDirectionBasics);
多個同級元素保持同一行(inline) -
自適應寬 引用官網源碼
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FlexDimensionsBasics extends Component { render() { return ( <View style={{flex: 1, flexDirection: 'row'}}> <View style={{flex: 1, backgroundColor: 'powderblue'}} /> <View style={{flex: 2, backgroundColor: 'skyblue'}} /> <View style={{flex: 3, backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
自適應寬 -
自適應高 引用官網源碼
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FlexDimensionsBasics extends Component { render() { return ( <View style={{flex: 1}}> <View style={{flex: 1, backgroundColor: 'powderblue'}} /> <View style={{flex: 2, backgroundColor: 'skyblue'}} /> <View style={{flex: 3, backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
自適應高 -
內間距(padding)
import React, { Component } from 'react'; import { AppRegistry, View, Text } from 'react-native'; class FlexDimensionsBasics extends Component { render() { return ( <View style={{flexDirection: 'row'}}> <Text style={{paddingLeft: 100, paddingTop: 200}}>Hello World!</Text> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
內間距(padding) -
外間距(margin)
import React, { Component } from 'react'; import { AppRegistry, View, Text } from 'react-native'; class FlexDimensionsBasics extends Component { render() { return ( <View style={{flex: 1}}> <View style={{margin: 100, width: 200, height: 200, backgroundColor: 'white'}}> <Text>Box 1</Text> <View style={{margin: 30, backgroundColor: 'powderblue'}}> <Text>Box 2</Text> </View> </View> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
外間距(margin)
-
對齊(align)
采用html的方式來實現向右對齊
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class JustifyContentBasics extends Component { render() { return ( <View> <View style={{width: 50, height: 50, backgroundColor: 'powderblue', position: 'absolute', right: 0}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);
采用flexDirection實現
row-reverse其實并非一種常規的向右對齊,而是反向對齊。import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class JustifyContentBasics extends Component { render() { return ( <View style={{flexDirection: 'row-reverse'}}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);
采用flexDirection+justifyContent
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class JustifyContentBasics extends Component { render() { return ( <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);
向右對齊上述三種都是向右對齊的例子,更多、更靈活的方法,請參考mozilla公司定義的flex-Direction、justify-Content標準。
mozilla justifyContent
-
固定位置
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class JustifyContentBasics extends Component { render() { return ( <View style={{flex: 1}}> <View style={{width: 50, height: 50, backgroundColor: 'powderblue', position: 'absolute', right: 100, bottom: 100}} /> </View> ); } }; AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);
固定位置 -
文本豎向居中
import React, { Component } from 'react'; import {View, AppRegistry, Text} from 'react-native'; class Bananaes extends Component { render() { return ( <View style={{height: 80, margin: 130, width: 80}}> <View style={{flex: 1, backgroundColor: "teal", justifyContent: 'center'}}> <Text>Hello</Text> </View> </View> ) } } AppRegistry.registerComponent('rn_practice', () => Bananaes);
文本豎向居中 -
文本橫向居中
import React, { Component } from 'react'; import {View, AppRegistry, Text} from 'react-native'; class Bananaes extends Component { render() { return ( <View style={{height: 80, margin: 130, width: 80}}> <View style={{flex: 1, backgroundColor: "teal", alignItems: 'center'}}> <Text>Hello</Text> </View> </View> ) } } AppRegistry.registerComponent('rn_practice', () => Bananaes);
文本橫向居中 -
文本橫向豎向居中
import React, { Component } from 'react'; import {View, AppRegistry, Text} from 'react-native'; class Bananaes extends Component { render() { return ( <View style={{height: 80, margin: 130, width: 80}}> <View style={{flex: 1, backgroundColor: "teal", alignItems: 'center', justifyContent: 'center'}}> <Text>Hello</Text> </View> </View> ) } } AppRegistry.registerComponent('rn_practice', () => Bananaes);
文本橫向居中