編輯:關於 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,取值為 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
marginBottom marginLeft marginRight marginTop marginVertical marginHorizontal margin
值得留意的是,marginVertical 等同於同時設置 marginTop 與 marginBottom,marginHorizontal 等同於同時設置 marginLeft 與 marginRight,而 margin 則等同於同時設置四個方向。
paddingBottom paddingLeft paddingRight paddingTop paddingVertical paddingHorizontal padding
屬性值同樣為字符串類型的 RGB 表示:
backgroundColor
borderTopLeftRadius borderTopRightRadius borderBottomLeftRadius borderBottomRightRadius borderRadius
width height
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 樣式表中所有可用屬性的梳理,掌握它們,日常的界面樣式基本都能應對自如。
BOS 專案 day58:以 Activiti 打造物流配送流程(啟動、查詢、辦理全串通)
一、設計物流配送流程 這一天的目標是把工作流的圖形化設計能力真正落到業務上:以組任務做任務分派,用排他網關做分支選擇,分支條件則由流程變數驅動。 Activiti 圖形
Android Studio 中 Git 的使用入門(一)
Git 與 GitHub Git 是分散式版本控制系統,適合管理多人協作的程式碼變更。GitHub 則是基於 Git 的遠端託管平台,可作為團隊同步與備份的遠端倉庫。 安
Android 音樂播放器(二):清單界面、播放控制與斜切轉場動畫
寫程式的過程中想法總在變,同一個功能常常糾結於「哪一種做法更好」。這個音樂播放器原本規劃把歌單頁與歌詞頁放進同一個 Activity 的兩個 Fragment,靠左右滑動
Android 自定義控件:進度條的四種實現方式
近來我一直在鑽研自定義控件,翻閱了不少前輩分享的教程,也在 GitHub 上找了一些類似控件來學習,只是讀起來都不太易懂,於是索性寫一篇學習筆記記錄下來。 一、控件介紹