Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

React Native 樣式表完全整理:可用屬性、定位與圖片顯示模式

編輯:關於 Android 開發

React Native 樣式表到底能寫什麼?

折騰了這麼多篇 Android 端 React Native 的筆記,架構層面比較硬骨頭的部分基本都啃下來了,接下來可以聊點輕鬆的——版面配置,也就是 Web 開發者最熟悉的 CSS。問題是:React Native 裡的樣式屬性和瀏覽器上的 CSS 完全一致嗎?還是有自己的一套?

驗證方式非常粗暴有效:在樣式表中隨手寫一個不存在的屬性,Packager 就會立刻報錯,告訴你這個 key 不存在,並順手把所有合法屬性名列全部印出來。

下面這份清單就是該版本樣式表支援的全部屬性,可以直接當速查表用:

"alignItems", "alignSelf", "backfaceVisibility", "backgroundColor",
"borderBottomColor", "borderBottomLeftRadius", "borderBottomRightRadius",
"borderBottomWidth", "borderColor", "borderLeftColor", "borderLeftWidth",
"borderRadius", "borderRightColor", "borderRightWidth", "borderStyle",
"borderTopColor", "borderTopLeftRadius", "borderTopRightRadius",
"borderTopWidth", "borderWidth", "bottom", "color", "flex", "flexDirection",
"flexWrap", "fontFamily", "fontSize", "fontStyle", "fontWeight", "height",
"justifyContent", "left", "letterSpacing", "lineHeight", "margin",
"marginBottom", "marginHorizontal", "marginLeft", "marginRight", "marginTop",
"marginVertical", "opacity", "overflow", "padding", "paddingBottom",
"paddingHorizontal", "paddingLeft", "paddingRight", "paddingTop",
"paddingVertical", "position", "resizeMode", "right", "rotation", "scaleX",
"scaleY", "shadowColor", "shadowOffset", "shadowOpacity", "shadowRadius",
"textAlign", "textDecorationColor", "textDecorationLine",
"textDecorationStyle", "tintColor", "top", "transform", "transformMatrix",
"translateX", "translateY", "width", "writingDirection"

樣式表的三種基本用法

先宣告,用 StyleSheet.create 把樣式集中管理:

var styles = StyleSheet.create({
  base: { width: 38, height: 38 },
  background: { backgroundColor: '#222222' },
  active: { borderWidth: 2, borderColor: '#00ff00' },
});

使用時透過 style 屬性引用單一樣式,也可以傳入陣列做合併:

<View style={styles.base} />
<View style={[styles.base, styles.background]} />

實務上更常見的是條件式套用。當 this.state.active 為 true 時套用 styles.active,否則不套用:

<View style={[styles.base, this.state.active ? styles.active : {}]} />

行內樣式(直接寫物件字面值)同樣可行,例如動態計算寬高比:

<View style={{ width: this.state.width, height: this.state.width / this.state.aspectRatio }} />

定位:relative 與 absolute

定位由 position 控制,可選值為 relative(相對定位,預設值)與 absolute(絕對定位)。兩者的差別在於「偏移的起算點」:相對定位以自己的原本位置為基準,絕對定位則以父容器(在 RN 中通常就是整個畫面)為基準。

先看絕對定位,四個方塊各自貼齊一個邊:

var styles = StyleSheet.create({
  container: { flex: 1, backgroundColor: '#ff0' },      // 黃色
  box1: { width: 50, height: 50, backgroundColor: '#f00',   // 紅
          position: 'absolute', left: 30 },
  box2: { width: 50, height: 50, backgroundColor: '#0f0',   // 綠
          position: 'absolute', top: 30 },
  box3: { width: 50, height: 50, backgroundColor: '#f0f',   // 紫
          position: 'absolute', right: 30 },
  box4: { width: 50, height: 50, backgroundColor: '#00f',   // 藍
          position: 'absolute', bottom: 30 },
});

若數值為負,方向就反過來,例如 left: -30 變成往左側再推出 30 個單位。

再來看相對定位。設了 top 與 left 之後,元素會從原本的位置往右、往下各移動 30:

box1: { width: 50, height: 50, backgroundColor: '#f00',
        position: 'relative', left: 30, top: 30 }

那如果相對定位寫的是 right 和 bottom 呢?

box1: { width: 50, height: 50, backgroundColor: '#f00',
        position: 'relative', right: 30, bottom: 30 }

效果等同於把 left、top 設為負值:距離原位右側 30、下側 30,也就是整體往左上平移 30 個單位。這裡的「原位」指的是完全不做定位偏移時的位置。

邊框、間距與尺寸

邊框寬度

borderBottomWidth   // 下邊框
borderLeftWidth     // 左邊框
borderRightWidth    // 右邊框
borderTopWidth      // 上邊框
borderWidth         // 四邊一次設定

邊框顏色

用法與寬度對應,值為字串形式的顏色表示(如 '#00ff00'、'rgb(0,255,0)'):

borderBottomColor  borderLeftColor  borderRightColor
borderTopColor     borderColor

外邊距

margin  marginTop  marginBottom  marginLeft  marginRight
marginVertical  marginHorizontal

注意這兩個複合屬性:marginVertical 等於同時設定上下,marginHorizontal 等於同時設定左右,margin 則是四邊一起。

內邊距

padding  paddingTop  paddingBottom  paddingLeft  paddingRight
paddingVertical  paddingHorizontal

背景色與圓角

backgroundColor
borderRadius  borderTopLeftRadius  borderTopRightRadius
borderBottomLeftRadius  borderBottomRightRadius

寬高

width  height

Flex 版面配置

React Native 只有 Flexbox 這一套版面模型,概念與 Web 上的 Flex 布局一致,推薦直接參考阮一峰的《Flex 布局教程:語法篇》與《實例篇》,講得相當清楚。可用屬性如下:

flex          number
flexDirection enum('row', 'column')
flexWrap      enum('wrap', 'nowrap')
alignItems    enum('flex-start', 'flex-end', 'center', 'stretch')
alignSelf     enum('auto', 'flex-start', 'flex-end', 'center', 'stretch')
justifyContent enum('flex-start', 'flex-end', 'center', 'space-between', 'space-around')

文字相關

color               文字顏色
fontFamily          字型家族
fontSize            字級
fontStyle           enum('normal', 'italic')
fontWeight          enum('normal','bold','100'...'900')
letterSpacing       字元間距
lineHeight          行高
textAlign           enum('auto','left','right','center','justify')
writingDirection    enum('auto','ltr','rtl')
textDecorationLine  enum('none','underline','line-through','underline line-through')
textDecorationStyle enum('solid','double','dotted','dashed')
textDecorationColor 底線顏色

實測時會發現 textDecorationStyle 與 textDecorationColor 在這個版本上效果不明顯,属于「寫了也不會怎樣」的類型,正式專案裡建議先驗證再依賴。

圖片顯示模式 resizeMode

三個可選值決定了圖片該如何適配容器:

resizeMode enum('cover', 'contain', 'stretch')
overflow   enum('visible', 'hidden')   // hidden 表示裁掉超出部分
tintColor  著色,接受顏色字串
opacity    透明度

contain 的規則是「圖片一定完整包含在容器內」:若容器比圖片大就置中顯示,若容器比較小則等比縮小。

var styles = StyleSheet.create({
  container: { backgroundColor: '#ff0' },
  img: { flex: 1, height: 150, resizeMode: 'contain' },
});

把高度改成 50,圖片就跟著等比縮小:

cover 是按實際尺寸填滿容器、超出的部分直接裁掉:

stretch 則是不管比例、直接拉伸填滿:

變形與陰影

scaleX      水平縮放
scaleY      垂直縮放
rotation    旋轉
translateX  水平平移
translateY  垂直平移
transform / transformMatrix  更完整的矩陣變形

shadowColor  shadowOffset  shadowOpacity  shadowRadius

整體看來,React Native 的樣式系統是「CSS 的精簡子集」:常用的邊框、間距、文字、Flex 都在,但像 display、float、偽類別選擇器這些 Web 老習慣就得先放下了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved