Andriod React Native 樣式表中可用樣式屬性

來源:互聯網
上載者:User

Andriod React Native 樣式表中可用樣式屬性

寫了這麼多篇Android React Native的博文,基本上把複雜的東西都搞定了,接下來來看看一些輕鬆的東西,和布局有關,就是css樣式,那麼一個View可以設定哪些css樣式呢,是和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',  },});

使用樣式

" data-snippet-id="ext.aa509d6f30a32f005f24a8d6e4ff9d13" data-snippet-saved="false" data-codota-status="done">

也可以接收一個數組

也可以根據條件來應用樣式

" data-snippet-id="ext.3bbd9d8827ab51d4f76bc1d214047d14" data-snippet-saved="false" data-codota-status="done">

假如this.state.active是true,styles.active就會被應用,如果為false,styles.active就不會被應用。

當然也是支援下面的這種寫法

" data-snippet-id="ext.f05f6af519aa220be500507b7324e73e" data-snippet-saved="false" data-codota-status="done">

接下來來講講樣式表中的具體屬性。

定位

定位分為相對定位和絕對位置,屬性名稱為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單位  }});

如下。

當對應的值為負數時,方向與原方向相反,即如果tZ喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcM6qLTUwo6zU8rvh1fu49tLGs/a1vcbBxLvN4qOoz/LJz9LGtb01MLj2taXOu6OpoaM8L3A+DQo8cD7Ex8O0z+C21LK8vtbT1srH1PXDtNH5tcTE2DwvcD4NCjxwcmUgY2xhc3M9"brush:java;">var AwesomeProject = React.createClass({ render: function() { return ( ); },});var styles = StyleSheet.create({ container:{ flex:1, backgroundColor:'#ff0'//黃色 }, box1:{ width:50, height:50, backgroundColor:'#f00',//紅色 position :'relative', left:30, top:30, },});

可以看到設定了top和left後,相對於不使用定位的位置向右向下移動了30單位,如果值為負數,也是往相反的方向移到。

但是如果設定了right和bottom後,又會怎麼樣呢

var styles = StyleSheet.create({  container:{    flex:1,    backgroundColor:'#ff0'//黃色  },  box1:{    width:50,    height:50,    backgroundColor:'#f00',//紅色    position :'relative',    right:30,    bottom:30,  },});

其實它的效果就是對應的top和left為負值的情況。距離原來位置的右側30單位,距離原來位置下側30單位,即向上向左平移30單位。原來位置就是不使用定位時的位置。

預設情況下使用的是相對定位

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

你可以使用設定所有邊框的寬度,也可以設定指定某條邊的寬度。

邊框顏色

同邊框寬度屬性,屬性值為字串類型的rgb表示方式

borderBottomColorborderLeftColorborderRightColorborderTopColorborderColor
外邊距
marginBottommarginLeftmarginRightmarginTopmarginVerticalmarginHorizontalmargin

值得注意的是marginVertical相當於同時設定marginTop和marginBottom,marginHorizontal相當於同時設定marginLeft和marginRight,margin相當於同時設定四個

內邊距
paddingBottom  paddingLeft  paddingRight  paddingTop  paddingVerticalpaddingHorizontal  padding 
背景色

背景色,屬性值為字串類型的rgb表示方式

backgroundColor
邊框圓角
borderTopLeftRadiusborderTopRightRadiusborderBottomLeftRadiusborderBottomRightRadiusborderRadius
寬高
width height
Flex布局相關

相關內容見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', '200', '300', '400', '500', '600', '700', '800', '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是指按實際大小進行顯示,超出部分進行裁剪,將屬性值改成cover之後的如下

stretch是指將映像進行展開顯示,將屬性值改為stretch後的效果如所示

映像變換
scaleX:水平方向縮放scaleY:垂直方向縮放rotation:旋轉translateX:水平方向平移translateY:水平方向平移
陰影
shadowColorshadowOffsetshadowOpacityshadowRadius

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.