編輯:關於 Android 程式設計
寫 React Native 時,很多人從 Android 原生轉過來會有點不適應——這裡沒有 LinearLayout,也沒有 RelativeLayout,取而代之的是一套來自 CSS 的 Flexbox 方案。好消息是,只要你理解「主軸」與「交叉軸」這兩個概念,RN 的佈局反而比 Android 那套嵌套體操要輕/jre又把qN? 得多了。
RN 目前支援的 Flexbox 屬性並不多,日常真正高頻使用的其實就六個:flexDirection、justifyContent、alignItems、alignSelf、flex、flexWrap。下面我們一個一個拆開來看,並配上實測效果圖。
flexDirection 用來指定主軸方向,也就是子元素排列的方向,只有兩個可選值:row(橫向,預設值)與 column(縱向)。
先看 row 的例子,父容器裡放四個子 View:
render: function() {
return (
<View style="{styles.flexStyle}">
<View style="{styles.flexSub}/">
<View style="{styles.flexSub}/">
<View style="{styles.flexSub}/">
<View style="{styles.flexSub}/">
</View>
);
}
var styles = StyleSheet.create({
flexStyle: {
height: 600,
flexDirection: 'row',
},
flexSub: {
flex: 1,
height: 300,
backgroundColor: '#333333',
marginRight: 10,
},
});
四個深灰色的小方塊依次橫向排開:

把 flexDirection 改成 column,同時把 marginRight 換成 marginBottom,樣式如下:
var styles = StyleSheet.create({
flexStyle: {
height: 600,
flexDirection: 'column',
},
flexSub: {
flex: 1,
height: 100,
backgroundColor: '#333333',
marginBottom: 10,
},
});
效果就變成縱向堆疊:

一句話記法:justifyContent 管主軸,alignItems 管交叉軸。當 flexDirection 是 row 時,justifyContent 控制水平方向;是 column 時則控制垂直方向。
它有五個可選值:
* flex-start:靠主軸起點對齊
* flex-end:靠主軸終點對齊
* center:主軸居中
* space-between:首個元素貼起點、末個元素貼終點,中間等間距分佈
* space-around:所有元素等間距分佈,且每個元素兩側都留有空隙
先看最常用的居中效果,父容器同時設定了 alignItems 與 justifyContent:
var styles = StyleSheet.create({
flexStyle: {
height: 600,
width: 400,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
flexSub: {
width: 100,
height: 300,
backgroundColor: '#333333',
},
});
子元素在容器中水平、垂直雙雙居中:

justifyContent: 'flex-start',水平靠左:

justifyContent: 'flex-end',水平靠右:

最容易搞混的是 space-between 和 space-around。前者兩端不留空隙:

後者兩端同樣留有空隙,每個元素的左右間距完全相等:

alignItems 用於定義子組件在交叉軸(這裡指垂直方向)上的對齊方式,共四個值:
* flex-start:與父容器頂部對齊
* flex-end:與父容器底部對齊
* center:垂直居中
* stretch:在垂直方向拉伸填滿整個容器(前提是子組件沒有顯式設定 height)
flex-start 的效果,為了看清楚,這裡給每個子塊換了不同顏色:

改成 flex-end 後,全部貼底對齊(只需修改 alignItems 一個屬性):

center 是實戰中使用頻率最高的值:

stretch 則是垂直方向完全撐滿:

alignSelf 允許某個子元素單獨覆寫父容器設定的 alignItems,可選值為 auto、flex-start、flex-end、center、stretch。其中 auto 表示沿用父容器的設定;若自身沒有指定寬高,表現等同 stretch。
var styles = StyleSheet.create({
flexStyle: {
height: 600,
flexDirection: 'row',
},
flexSelf1: { flex: 1, alignSelf: 'flex-start', height: 300, backgroundColor: '#ff0000', marginRight: 10 },
flexSelf2: { flex: 1, alignSelf: 'flex-end', height: 300, backgroundColor: '#00ff00', marginRight: 10 },
flexSelf3: { flex: 1, alignSelf: 'stretch', backgroundColor: '#0000ff', marginRight: 10 },
flexSelf4: { flex: 1, alignSelf: 'auto', height: 300, backgroundColor: '#333333', marginRight: 10 },
});
四個子塊各自對齊方式不同,效果一目了然:

第一個頂對齊、第二個底對齊、第三個因為沒寫 height 而被垂直拉伸、第四個是 auto 且指定了高度所以照原樣顯示。center 的效果同理,這裡就不再演示。
flex 決定元素如何分配剩餘空間,可以把它理解為 Android 裡的 layout_weight。
var styles = StyleSheet.create({
flexStyle: { height: 600, flexDirection: 'row', flex: 1 },
flexSelf1: { flex: 1, backgroundColor: '#ff0000', marginRight: 10 },
flexSelf2: { flex: 2, backgroundColor: '#00ff00', marginRight: 10 },
flexSelf3: { flex: 4, backgroundColor: '#0000ff', marginRight: 10 },
});
比例關係非常直觀——flex 為 2 的寬度是 flex 為 1 的兩倍,flex 為 4 則是四倍:

flexWrap 控制主軸上元素排不下時的行為,兩個值:nowrap(預設,空間不足也不換行,元素會被壓縮)與 wrap(空間不足自動換行)。
設為 wrap 後,第四個元素放不下就自動掉到下一行:

這六個屬性背後的邏輯其實只有一句話:先確定主軸(flexDirection),再決定主軸上怎麼分佈(justifyContent)與交叉軸上怎麼對齊(alignItems),需要特例時用 alignSelf 打補丁,需要分配空間時用 flex,需要換行時用 flexWrap。
這些屬性原本都來自 CSS,只是 RN 目前僅支援其中一部分(例如 display: grid、float 之類暫時還不能用)。把上面幾張對比圖記下來,日常九成的畫面都能拼得出來。
在 Android Studio 中生成 .so 庫的完整實踐
簡介 本文記錄了在 Android Studio 中生成 so 庫的實踐過程。上一篇記錄的是通過 Eclipse 生成 so 的方法,這裡則記錄一下如何在 Android
Material Design 新 UI 控件用法大全(二):TabLayout 與 CoordinatorLayout
序言 上一篇介紹了幾個比較基礎的新控件,相信大家对 Material Design 的視覺語言已經有了初步認識。這次我們繼續往下挖,看看 CoordinatorLayou
Android應用ViewDragHelper詳解及部分源碼淺析
1 背景 很久沒更新博客了,這次忙裡偷閒產出一篇。寫這篇文章主要是源於去年項目中的一個需求,當時三下五除二就把功能實現了,但源碼閱讀卻一直擱置著沒時間理會,現在正好撿起來
Android 開發實戰:使用 DOM 方式解析 XML
有別於 SAX 和 PULL 兩種解析方式,DOM 解析會先把整個 XML 檔案全部載入記憶體,再組裝成一棵 DOM 樹,然後透過節點以及節點之間的關聯關係來解析 XML