1. 程式人生 > >React Native探索(四)Flexbox佈局詳解

React Native探索(四)Flexbox佈局詳解

前言

在Android開發中我們有很多種佈局,比如LinearLayout和RelativeLayout,同樣在React Native也有它的佈局,這個佈局就是Flexbox佈局。在CSS、React Native和Android等都有它的身影。這一篇文章,我們就通過各種小例子來掌握React Native中的Flexbox佈局。

1.Flexbox佈局概述

Flexbox譯為彈性佈局(這裡我們簡稱Flex),是CSS的一種佈局方案,可以簡單、完整、響應式的實現各種頁面佈局。不只是在CSS中應用,在React Native也使用了Flex,基本和CSS中的Flex類似。甚至在Android開發中我們也會用到Flex,谷歌提供了基於Flex的

FlexboxLayout,以便於處理複雜的佈局,因此,學習Flex佈局對於Android開發也是有幫助的。
採用Flex佈局的元素,稱為Flex容器(flex container),簡稱容器,它的所有子元素則是Flex容器的成員稱為Flex專案(flex item),簡稱專案。如下圖所示。

容器預設存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置叫做main start,結束位置叫做main end。相似的,交叉軸的開始位置叫做cross start,結束位置叫做cross end。專案預設沿主軸排列,它在主軸上的長度叫做main size,交叉軸上的長度叫做cross size。

2.Flexbox容器屬性

在CSS(React)中容器屬性有6種,而在React Native中容器屬性有5種,它們分別是:

  • flexDirection
  • justifyContent
  • alignItems
  • alignContent
  • flexWrap

下面通過小例子來分別介紹這些Flexbox容器屬性。

flexDirection

flexDirection屬性可以決定主軸的方向(即專案的排列方向),它有以下取值:

  • column(預設值):主軸為垂直方向,起點在頂端。
  • row:主軸為水平方向,起點在左端。
  • column-reverse:主軸為垂直方向,起點在下端。
  • row-reverse:主軸為水平方向,起點在右端。

我們先將flexDirection設定為row,程式碼如下所示。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{flex: 1, flexDirection: 'row', backgroundColor: 'ivory'}}>
                <View style={{width: 60, height: 60, backgroundColor: 'powderblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'skyblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'dodgerblue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

執行效果如下圖所示。
QQ圖片20170524191908.jpg

可以看出專案(子元件)是水平排列的,並且起點在左端。關於例子中的顏色設定可以檢視官網文件。我們也可以將flexDirection設定為row-reverse,來檢視效果:
QQ圖片20170524200617.jpg
可以看出Flex專案同樣是水平排列的,只是起點在右端。

justifyContent

justifyContent屬性用於定義專案在主軸上的對齊方式。它的取值有以下幾種:

  • flex-start(預設值):專案與父容器在主軸的起點方向對齊。(比如flexDirection等於row時,專案與父容器左端對齊)
  • flex-end:專案與父容器在主軸的終點方向對齊(比如flexDirection等於row時,專案與父容器右端對齊)。
  • center:居中。
  • space-between: 兩端對齊,並且專案間隔相等。
  • space-around:每個專案的兩側間隔相等,因此,專案之間的間隔是專案與父容器邊緣間隔的2倍。

我們將justifyContent設定為flex-end,程式碼如下所示。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-end', backgroundColor: 'ivory'}}>
                <View style={{width: 60, height: 60, backgroundColor: 'powderblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'skyblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'dodgerblue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

效果如下所示。
QQ圖片20170524215354.jpg

接下來我們分別設定justifyContent為flex-startcenter,效果分別如下所示。

QQ圖片20170524220218.jpg …… QQ圖片20170524220103.jpg

接下來我們分別設定justifyContent為space-betweenspace-around來檢視它們有什麼區別,效果分別如下所示。

QQ圖片20170524220851.jpg …… QQ圖片20170524221540.jpg

上面左圖是設定了space-between,可以看出最左邊和最右邊的專案都和父容器沒有間隔,並且專案之間的間隔是相等的。右圖的是space-around,最左邊和最右邊的專案都和父容器有間隔,並且專案之間的間隔是專案與父容器的間隔的2倍。

alignItems

alignItems用於定義專案在交叉軸上的對齊方式。它的取值有以下幾種:

  • flex-start:專案與父容器在交叉軸的起點方向對齊。(比如flexDirection等於row時,專案與父容器上端對齊)
  • flex-end:專案與父容器在交叉軸的終點方向對齊。(比如flexDirection等於row時,專案與父容器下端對齊)
  • center:居中。
  • baseline :專案的第一行文字的基線對齊。
  • stretch:(預設值)如果專案未設定高度或者高度設為auto,專案將佔滿整個容器的高度,否則該取值不會生效。

將alignItems設定為flex-end,程式碼如下所示。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                justifyContent: 'center',
                alignItems: 'flex-end',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 60, height: 60, backgroundColor: 'powderblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'skyblue'}}/>
                <View style={{width: 60, height: 60, backgroundColor: 'dodgerblue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

效果如下圖所示。
QQ圖片20170525125023_副本.jpg

看到flex-end的效果,flex-start和center的效果也很容易知道。我們接下來將alignItems設定為stretch,需要注意的是,當專案沒有設定高度或者高度設為auto時,stretch才會生效。這裡為了驗證效果,將所有專案的高度設定為auto。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                justifyContent: 'center',
                alignItems: 'stretch',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 60, height: 'auto', backgroundColor: 'powderblue'}}/>
                <View style={{width: 60, height: 'auto', backgroundColor: 'skyblue'}}/>
                <View style={{width: 60, height: 'auto', backgroundColor: 'dodgerblue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

效果如下圖所示。
QQ圖片20170525131620_副本.jpg
可以看出,當alignItems設定為stretch時,專案會佔滿整個容器的高度。

alignContent

alignContent用於多行專案在交叉軸上的對齊方式。如果專案只有一行,該屬性是不起作用的。它的取值有 flex-start 、flex-end 、 center 、space-between 、 space-around 和 stretch,只比justifyContent的取值多了一個stretch(預設值,含義和alignItems的stretch類似),alignContent的取值的含義和justifyContent的取值的含義類似,這裡就不做舉例了。

flexWrap

flexWrap用於設定如果一行排不下,如何換行。它的取值有以下幾種:
- nowrap(預設):不換行。
- wrap:換行,第一行在上方。

我們將flexWrap設定為wrap,程式碼如下所示。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                alignItems: 'flex-start',
                flexWrap: 'wrap',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 100, height: 60, backgroundColor: 'powderblue'}}/>
                <View style={{width: 100, height: 60, backgroundColor: 'skyblue'}}/>
                <View style={{width: 100, height: 60, backgroundColor: 'dodgerblue'}}/>
                <View style={{width: 100, height: 60, backgroundColor: 'blue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

效果如下所示。

QQ圖片20170525141454.jpg

3.Flexbox專案屬性

在React Native中專案屬性有很多中,具體的可以參考:Layout Props。這裡介紹flexGrow、flexShrink、flexBasis、flex和alignSelf。

flexGrow

flexGrow屬性定義專案的放大比例,預設為0,即如果存在剩餘空間,也不放大。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                alignItems: 'flex-start',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 50, height: 50, flexGrow: 1, backgroundColor: 'powderblue'}}/>
                <View style={{width: 50, height: 50, flexGrow: 2, backgroundColor: 'skyblue'}}/>
                <View style={{width: 50, height: 50, flexGrow: 1, backgroundColor: 'dodgerblue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

我們將第二個專案flexGrow設定為2,其他的專案flexGrow設定為1,這樣第二個專案所佔的剩餘空間是其他專案的兩倍。如下圖所示。
QQ圖片20170525150041.jpg

flexShrink

flexShrink屬性定義了專案的縮小比例,預設為1,即如果空間不足,該專案將縮小。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                alignItems: 'flex-start',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 120, height: 50, flexShrink: 1, backgroundColor: 'powderblue'}}/>
                <View style={{width: 120, height: 50, flexShrink: 0, backgroundColor: 'skyblue'}}/>
                <View style={{width: 120, height: 50, flexShrink: 1, backgroundColor: 'dodgerblue'}}/>
                <View style={{width: 120, height: 50, flexShrink: 1, backgroundColor: 'blue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

我們將第二個專案的flexShrink設定為0,其他的專案都為1,這樣當空間不足時,第二個專案不會縮小,如下圖所示。

QQ圖片20170525151535.jpg

flexBasis

flexBasis屬性定義了專案的初始寬度。它的預設值為auto,即專案的本來的寬度。我們知道width也可以用來設定專案的寬度,如果專案同時設定了width和flexBasis,那麼flexBasis會覆蓋width的值。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                alignItems: 'flex-start',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 120, height: 50, flexBasis: 60, backgroundColor: 'powderblue'}}/>
                <View style={{width: 120, height: 50, backgroundColor: 'skyblue'}}/>
                <View style={{width: 120, height: 50, backgroundColor: 'dodgerblue'}}/>
                <View style={{width: 120, height: 50, backgroundColor: 'blue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

效果如下圖所示。

QQ圖片20170525164514.jpg

flex

如果我們每次都要設定flex-grow、flex-shrink和 flex-basis屬性,顯然有些麻煩,這時我們可以用flex 屬性,它是 flex-grow、flex-shrink 和 flex-basis 屬性的簡寫屬性,預設值為0 1 auto,其中後兩個屬性可選。關於flex這裡就不做舉例了。

alignSelf

alignSelf屬性和alignItems屬性類似,只不過alignSelf作用於專案,它允許單個專案有與其他專案不一樣的對齊方式,並且覆蓋alignItems屬性。alignSelf預設值為為auto,表示繼承父元素的alignItems屬性,如果沒有父元素,則等同於stretch。alignSelf有五種取值:auto、flex-start、flex-end、center、baseline和stretch,除了多了auto,其他的取值都和alignItems的取值含義一樣。

import React, {Component} from 'react';
import {AppRegistry, View} from 'react-native';
class FlexDirection extends Component {
    render() {
        return (
            <View style={{
                flex: 1,
                flexDirection: 'row',
                alignItems: 'flex-start',
                backgroundColor: 'ivory'
            }}>
                <View style={{width: 60, height: 60, alignSelf: 'flex-end', backgroundColor: 'powderblue'}}/>
                <View style={{width: 60, height: 60, alignSelf: 'center', backgroundColor: 'skyblue'}}/>
                <View style={{width: 60, height: 'auto', alignSelf: 'stretch', backgroundColor: 'dodgerblue'}}/>
                <View style={{width: 60, height: 60, alignSelf: 'auto', backgroundColor: 'blue'}}/>
            </View>
        );
    }
}
AppRegistry.registerComponent('AwesomeProject', () => FlexDirection);

執行效果如下所示。
QQ圖片20170526192438_副本.jpg

好了,關於Flexbox佈局就講到這,還有很多屬性這裡沒有提到,比如:margin、padding、marginRight和maxWidth等等,這些屬性我們一看名字就知道它的作用(Android開發者角度),因此這裡就不多介紹了,更多的屬性請查閱官方文件

歡迎關注我的微信公眾號,第一時間獲得部落格更新提醒,以及更多成體系的Android相關原創技術乾貨。
掃一掃下方二維碼或者長按識別二維碼,即可關注。

相關推薦

React Native探索Flexbox佈局

前言 在Android開發中我們有很多種佈局,比如LinearLayout和RelativeLayout,同樣在React Native也有它的佈局,這個佈局就是Flexbox佈局。在CSS、React Native和Android等都有它的身影。這一

React Native探索佈局

可以看到iphone 6的寬度為 375pt,對應了上邊的375,由此可見react的單位為pt。 那如何獲取實際的畫素尺寸呢? 這對圖片的高清化很重要,如果我的圖片大小為100*100 px. 設定寬度為100 * 100. 那在iphone上的尺寸就是模糊的。 這個時候需要的影象大小應該是 100 *

React Native入門之使用Flexbox佈局

前言 flex,收縮,彈性的意思。 彈性(Flex)寬高 關於RN中寬高的設定,我們在上一篇設定Image載入網路圖片的時候提到過,首先width和height是兩個屬性,用來指定一個元件的寬高,使用的時候可以這樣: <Image source

React Native探索使用fetch進行網絡請求

數據處理 boolean from convert light util javascrip content import 前言 React Native可以使用多種方式來進行網絡請求,比如fetch、XMLHttpRequest以及基於它們封裝的框架,fetch可以說是替

React-Native 基礎使用style定義元件的樣式

style是一個props style的鍵值命名格式遵循CSS風格,除了名字使用駝峰法則而不是使用分隔符。例如背景色:backgoundColor,不是background-color 可以傳遞style陣列,最後一個style有優先權,因而可以使用它繼承

React Native探索不止是UI:React的使用場景探索

藉助 atom-shell 或者 node-webkit 這類專案,我們可以在桌面上執行一個 Web應用。來自 Github 的 Atom Editor 就是使用 atom-shell 以及 React建立的。 下面將 atom-shell 應用於我們的SurveyBuilder。 首先,從這裡下載並且安裝

Hibernate API

delet hibernate load 類型變量 nbsp ria 每次 transacti llb 一、Configuration類 用來加載默認文件路徑下的配置文件(hibernate.properties)。 調用configure()方法會加載默認文件路徑下的xm

Spring Cloud學習Zuul過濾器

轉載自:http://www.itmuch.com/spring-cloud/zuul/spring-cloud-zuul-filter/     http://blog.didispace.com/spring-cloud-zuul-exception-2/     http://blog

Jenkins學習job介面

一.建立job job安裝後,點選new item會出現如下介面,這個介面用於建立不用風格的job 1.Freestyle project 這個是jenkins的基礎功能,可以用它來執行各種構建任務,他只能構建在一個電腦上,如果沒有太多的需求,這個jo

Kafka 系列—— Kafka 消費者

一、消費者和消費者群組 在 Kafka 中,消費者通常是消費者群組的一部分,多個消費者群組共同讀取同一個主題時,彼此之間互不影響。Kafka 之所以要引入消費者群組這個概念是因為 Kafka 消費者經常會做一些高延遲的操作,比如把資料寫到資料庫或 HDFS ,或者進行耗時的計算,在這些情況下,單個消費者無法跟

React Native佈局使用Flexbox

歡迎一起來學習React Native,QQ群:672509442 簡介 我們在React Native中使用flexbox規則來指定某個元件的子元素的佈局。Flexbox可以在不同螢幕尺寸上提供一致的佈局結構。相對於Native開發的佈局更加

React Native學習筆記Flex佈局

1、基本樣式 對於一個元件,定義元件的佈局樣式通過style屬性來定義。 例如: <Text style = {{color: '#ff0000', fontSize: 15}}> 學習佈局 </Text> 這裡通過style

React Native學習—— 使用Flexbox布局

styles BE art 分享圖片 category urn def ger p s 本文基於React Native 0.52 Demo上傳到Git了,有需要可以看看,寫了新內容會上傳的。Git地址 https://github.com/gingerJY/React-N

數據庫類型空間效率探索-tinyint與enum與set

數據 ngs truncate 類型 column 效率 select name type mysql> select count(*) from userinfo;+----------+| count(*) |+----------+| 115597 |+--

React Native學習—— 對接七魚客服

clas render round 外部文件 bubuko source his 代碼 veh 本文基於React Native 0.52 Demo上傳到Git了,有需要可以看看,寫了新內容會上傳的。Git地址 https://github.com/gingerJY/Rea

react-native 學習

androi bsp ger net devel 瀏覽器 sim 百度 解決方法 上一節講到了 react-native的開發環境的配置,,這一節我門具體講講怎麽看樣式,怎麽調試 看樣式的話 有一個 神奇 react-native-developer tools(個人推薦,

react-native 學習

-s 樣式 dimens screen gpo 像素 github php 我們 上一次講到了react-native 的配置環境 和 如何去進行調試,這一次我們說一說,關於react-native的 樣式兼容問題。 由於iphonex的發售,在兼容的時候,我門也需要去考慮

使用Formik輕松開發更高質量的React表單其他幾個API解析

else errors method however obj disable user etc gree (下面部分內容正處於翻譯中,敬請稍等......) <Field /> <Field /> will automagically hook up

react-native + teasetDrawer實現側邊菜單

() ava npr img nat width let vector bottom 1.代碼 /** * 購物車 */ import React, {Component} from ‘react‘; import { View, Image, } from

04 React快速入門——元件拆分

問題描述:       在目前存在的例項中,程式碼結構如下圖所示:        在index.js中引入了TodoList.js中定義的元件來實現頁面上的一個簡單佈局,一個輸入框和一個按鈕,通過點選按鈕來依次將