寬度單位和像素密度
react的寬度不支持百分比,設置寬度時不需要帶單位 {width: 10}
, 那么10代表的具體寬度是多少呢?
不知道是官網文檔不全還是我眼瞎,反正是沒找到,那做一個實驗自己找吧:
var Dimensions = require('Dimensions'); <Text style={styles.welcome}> window.width={Dimensions.get('window').width + '\n'} window.height={Dimensions.get('window').height + '\n'} pxielRatio={PixelRatio.get()} </Text>
默認用的是ihone6的模擬器結果是:
window.width=375 window.height=667
我們知道iphone系列的尺寸如下圖:
可以看到iphone 6的寬度為 375pt,對應了上邊的375,實際上官文指出的單位為 dp 。 那如何獲取實際的像素尺寸呢? 這對圖片的高清化很重要,如果我的圖片大小為100100 px. 設置寬度為100 100. 那在iphone上的尺寸就是模糊的。 這個時候需要的圖像大小應該是 100 * pixelRatio的大小 。
react 提供了PixelRatio 的獲取方式https://facebook.github.io/react-native/docs/pixelratio.html
var image = getImage({ width: 200 * PixelRatio.get(), height: 100 * PixelRatio.get() }); <Image source={image} style={{width: 200, height: 100}} />
flex的布局
默認寬度
我們知道一個div如果不設置寬度,默認的會占用100%的寬度, 為了驗證100%這個問題, 做三個實驗
-
根節點上方一個View, 不設置寬度
-
固定寬度的元素上設置一個View, 不設置寬度
-
flex的元素上放一個View寬度, 不設置寬度
<Text style={[styles.text, styles.header]}> 根節點上放一個元素,不設置寬度 </Text> <View style={{height: 20, backgroundColor: '#333333'}} /> <Text style={[styles.text, styles.header]}> 固定寬度的元素上放一個View,不設置寬度 </Text> <View style={{width: 100}}> <View style={{height: 20, backgroundColor: '#333333'}} /> </View> <Text style={[styles.text, styles.header]}> flex的元素上放一個View,不設置寬度 </Text> <View style={{flexDirection: 'row'}}> <View style={{flex: 1}}> <View style={{height: 20, backgroundColor: '#333333'}} /> </View> <View style={{flex: 1}}/> </View>
結果可以看到flex的元素如果不設置寬度, 都會百分之百的占滿父容器。
水平垂直居中
css 里邊經常會做的事情是去講一個文本或者圖片水平垂直居中,如果使用過css 的flexbox當然知道使用alignItems
和 justifyContent
. 那用react-native也來做一下實驗
<Text style={[styles.text, styles.header]}> 水平居中 </Text> <View style={{height: 100, backgroundColor: '#333333', alignItems: 'center'}}> <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/> </View> <Text style={[styles.text, styles.header]}> 垂直居中 </Text> <View style={{height: 100, backgroundColor: '#333333', justifyContent: 'center'}}> <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/> </View> <Text style={[styles.text, styles.header]}> 水平垂直居中 </Text> <View style={{height: 100, backgroundColor: '#333333', alignItems: 'center', justifyContent: 'center'}}> <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/> </View>
網格布局
網格布局實驗, 網格布局能夠滿足絕大多數的日常開發需求,所以只要滿足網格布局的spec,那么就可以證明react的flex布局能夠滿足正常開發需求
等分的網格
<View style={styles.flexContainer}>
<View style={styles.cell}> <Text style={styles.welcome}> cell1 </Text> </View> <View style={styles.cell}> <Text style={styles.welcome}> cell2 </Text> </View> <View style={styles.cell}> <Text style={styles.welcome}> cell3 </Text> </View> </View> styles = { flexContainer: { // 容器需要添加direction才能變成讓子元素flex flexDirection: 'row' }, cell: { flex: 1, height: 50, backgroundColor: '#aaaaaa' }, welcome: { fontSize: 20, textAlign: 'center', margin: 10 }, }
左邊固定, 右邊固定,中間flex的布局
<View style={styles.flexContainer}>
<View style={styles.cellfixed}> <Text style={styles.welcome}> fixed </Text> </View> <View style={styles.cell}> <Text style={styles.welcome}> flex </Text> </View> <View style={styles.cellfixed}> <Text style={styles.welcome}> fixed </Text> </View> </View> styles = { flexContainer: { // 容器需要添加direction才能變成讓子元素flex flexDirection: 'row' }, cell: { flex: 1, height: 50, backgroundColor: '#aaaaaa' }, welcome: { fontSize: 20, textAlign: 'center', margin: 10 }, cellfixed: { height: 50, width: 80, backgroundColor: '#fefefe' } }
嵌套的網格
通常網格不是一層的,布局容器都是一層套一層的, 所以必須驗證在real world
下面的網格布局
<Text style={[styles.text, styles.header]}> 嵌套的網格 </Text> <View style={{flexDirection: 'row', height: 200, backgroundColor:"#fefefe", padding: 20}}> <View style={{flex: 1, flexDirection:'column', padding: 15, backgroundColor:"#eeeeee"}}> <View style={{flex: 1, backgroundColor:"#bbaaaa"}}> </View> <View style={{flex: 1, backgroundColor:"#aabbaa"}}> </View> </View> <View style={{flex: 1, padding: 15, flexDirection:'row', backgroundColor:"#eeeeee"}}> <View style={{flex: 1, backgroundColor:"#aaaabb"}}> <View style={{flex: 1, flexDirection:'row', backgroundColor:"#eeaaaa"}}> <View style={{flex: 1, backgroundColor:"#eebbaa"}}> </View> <View style={{flex: 1, backgroundColor:"#bbccee"}}> </View> </View> <View style={{flex: 1, backgroundColor:"#eebbdd"}}> </View> </View> <View style={{flex: 1, backgroundColor:"#aaccaa"}}> <ScrollView style={{flex: 1, backgroundColor:"#bbccdd", padding: 5}}> <View style={{flexDirection: 'row', height: 50, backgroundColor:"#fefefe"}}> <View style={{flex: 1, flexDirection:'column', backgroundColor:"#eeeeee"}}> <View style={{flex: 1, backgroundColor:"#bbaaaa"}}> </View> <View style={{flex: 1, backgroundColor:"#aabbaa"}}> </View> </View> <View style={{flex: 1, flexDirection:'row', backgroundColor:"#eeeeee"}}> <View style={{flex: 1, backgroundColor:"#aaaabb"}}> <View style={{flex: 1, flexDirection:'row', backgroundColor:"#eeaaaa"}}> <View style={{flex: 1, backgroundColor:"#eebbaa"}}> </View> <View style={{flex: 1, backgroundColor:"#bbccee"}}> </View> </View> <View style={{flex: 1, backgroundColor:"#eebbdd"}}> </View> </View> <View style={{flex: 1, backgroundColor:"#aaccaa"}}> </View> </View> </View> <Text style={[styles.text, styles.header, {color: '#ffffff', fontSize: 12}]}> {(function(){ var str = ''; var n = 100; while(n--) { str += '嵌套的網格' + '\n'; } return str; })()} </Text> </ScrollView> </View> </View>