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

React Native 樣式表全解析:可用屬性清單與實際用法

編輯:關於 Android 程式設計

前面寫了一系列 Android React Native 的文章,較為複雜的內容基本都梳理完了,這次換點輕鬆的話題——樣式。也就是我們熟悉的 CSS 樣式,那麼一個 View 究竟能設置哪些樣式屬性?它和 Web 中的 CSS 完全一致嗎,還是有差別?

其實驗證方法很簡單:只要你在樣式表裡隨手寫一個不存在的屬性,它就會立刻報一大堆錯誤,提示該樣式並不存在,並順手把所有可用的樣式屬性都列出來給你參考,如下圖。

下面這份清單,就是樣式表中所有可用的屬性:

"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"

接下來我們逐項解釋。但在這之前,先說說 React 中樣式表的幾種基本用法。

樣式的聲明

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

樣式的使用

可以直接引用單個樣式,也可以傳入一個數組組合多個樣式,還能根據條件動態應用。例如當 this.state.active 為 true 時,styles.active 就會生效;為 false 時則不套用。

除此之外,也支持在樣式中寫入計算表達式的寫法。

定位(position)

定位分為相對定位與絕對定位,屬性名為 position,取值為 absolute 和 relative。使用絕對定位時,元素相對於屏幕進行定位。

var AwesomeProject = React.createClass({
  render: function() {
    return (
      
        
          
          
          
          
        
      
    );
  },
});
var styles = StyleSheet.create({
  container: { flex: 1, backgroundColor: '#ff0' }, //黃色
  box1: { width: 50, height: 50, backgroundColor: '#f00', position: 'absolute', left: 30 },   //紅色,距屏幕左側 30
  box2: { width: 50, height: 50, backgroundColor: '#0f0', position: 'absolute', top: 30 },    //綠色,距屏幕上側 30
  box3: { width: 50, height: 50, backgroundColor: '#f0f', position: 'absolute', right: 30 },  //紫色,距屏幕右側 30
  box4: { width: 50, height: 50, backgroundColor: '#00f', position: 'absolute', bottom: 30 },  //藍色,距屏幕下側 30
});

效果如下圖。當對應的值為負數時,方向相反,例如 top 為 -50,元素就會向上移出屏幕 50 個單位。

那相對定位又是怎麼回事呢?設置了 top 和 left 後,元素會相對於原本的位置向右、向下平移對應的距離;若值為負數,則往相反方向移動。

而如果設置的是 right 和 bottom,效果等同於 top 和 left 取負值——也就是向上、向左平移。所謂「原來的位置」,指的便是不使用定位時元素本該所在的位置。

默認情況下使用的就是相對定位。

邊框寬度

borderBottomWidth //底部邊框寬度
borderLeftWidth   //左邊邊框寬度
borderRightWidth  //右邊邊框寬度
borderTopWidth    //頂部邊框寬度
borderWidth       //所有邊框寬度

你可以一次性設置所有邊框的寬度,也可以只針對某一條邊單獨設置。

邊框顏色

用法與邊框寬度類似,屬性值為字符串類型的 RGB 表示:

borderBottomColor borderLeftColor borderRightColor borderTopColor borderColor

外邊距(margin)

marginBottom marginLeft marginRight marginTop marginVertical marginHorizontal margin

值得留意的是,marginVertical 等同於同時設置 marginTop 與 marginBottom,marginHorizontal 等同於同時設置 marginLeft 與 marginRight,而 margin 則等同於同時設置四個方向。

內邊距(padding)

paddingBottom paddingLeft paddingRight paddingTop paddingVertical paddingHorizontal padding

背景色

屬性值同樣為字符串類型的 RGB 表示:

backgroundColor

邊框圓角

borderTopLeftRadius borderTopRightRadius borderBottomLeftRadius borderBottomRightRadius borderRadius

寬高

width height

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')
textDecorationLine   修飾線,enum("none", 'underline', 'line-through', 'underline line-through')
textDecorationStyle  enum("solid", 'double', 'dotted', 'dashed')
textDecorationColor  修飾線的顏色
writingDirection enum("auto", 'ltr', 'rtl')

圖片相關屬性

resizeMode enum('cover', 'contain', 'stretch')
overflow   enum('visible', 'hidden')  //超出部分是否顯示,hidden 為隱藏
tintColor  著色,RGB 字符串類型
opacity    透明度

其中 resizeMode 的三個取值需要重點理解。contain 表示無論如何都讓圖片完整包含在指定區域內:若設置的寬高比圖片大,圖片居中顯示;否則等比縮小顯示。

var AwesomeProject = React.createClass({
  render: function() {
    return (
      
        
      
    );
  },
});
var styles = StyleSheet.create({
  container: { backgroundColor: '#ff0' }, //黃色
  img: { flex: 1, height: 150, resizeMode: "contain" },
});

效果如下圖;把高度改成 50 後,圖片相應縮小:

cover 按實際大小顯示,超出部分進行裁剪:

stretch 則是將圖像拉伸鋪滿顯示:

圖像變換

scaleX     水平方向縮放
scaleY     垂直方向縮放
rotation   旋轉
translateX 水平方向平移
translateY 垂直方向平移

陰影

shadowColor shadowOffset shadowOpacity shadowRadius

以上就是 React Native 樣式表中所有可用屬性的梳理,掌握它們,日常的界面樣式基本都能應對自如。

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